home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / utility / emxinfo.zip / MAKEINFO.C < prev    next >
C/C++ Source or Header  |  1992-03-16  |  174KB  |  6,857 lines

  1. /* Makeinfo -- convert texinfo format files into info files
  2.  
  3.    Copyright (C) 1987, 1991 Free Software Foundation, Inc.
  4.  
  5.    This file is part of GNU Info.
  6.  
  7.    Makeinfo is distributed in the hope that it will be useful,
  8.    but WITHOUT ANY WARRANTY.  No author or distributor accepts
  9.    responsibility to anyone for the consequences of using it or for
  10.    whether it serves any particular purpose or works at all, unless he
  11.    says so in writing.  Refer to the GNU Emacs General Public License
  12.    for full details.
  13.  
  14.    Everyone is granted permission to copy, modify and redistribute
  15.    Makeinfo, but only under the conditions described in the GNU Emacs
  16.    General Public License.   A copy of this license is supposed to
  17.    have been given to you along with GNU Emacs so you can know your
  18.    rights and responsibilities.  It should be in a file named COPYING.
  19.    Among other things, the copyright notice and this notice must be
  20.    preserved on all copies.  */
  21.  
  22. /* This is Makeinfo version 1.43.  If you change the version number of
  23.    Makeinfo, please change it here and at the lines reading:
  24.  
  25.     int major_version = 1;
  26.     int minor_version = 43;
  27.  
  28.    in the code below. */
  29.  
  30. /* You can change some of the behaviour of Makeinfo by changing the
  31.    following defines: */
  32.  
  33. /* Define INDENT_PARAGRAPHS_IN_TABLE if you want the paragraphs which
  34.    appear within an @table, @ftable, or @itemize environment to have
  35.    standard paragraph indentation.  Without this, such paragraphs have
  36.    no starting indentation. */
  37. /* #define INDENT_PARAGRAPHS_IN_TABLE */
  38.  
  39. /* Define DEFAULT_INDENTATION_INCREMENT as an integer which is the amount
  40.    that @example should increase indentation by.  This incrmement is used
  41.    for all insertions which indent the enclosed text. */
  42. #define DEFAULT_INDENTATION_INCREMENT 5
  43.  
  44. /* Define PARAGRAPH_START_INDENT to be the amount of indentation that
  45.    the first lines of paragraphs receive by default, where no other
  46.    value has been specified.  Users can change this value on the command
  47.    line, with the +paragraph-indent option, or within the texinfo file,
  48.    with the @paragraphindent command. */
  49. #define PARAGRAPH_START_INDENT 3
  50.  
  51. /* Define DEFAULT_PARAGRAPH_SPACING as the number of blank lines that you
  52.    wish to appear between paragraphs.  A value of 1 creates a single blank
  53.    line between paragraphs.  Paragraphs are defined by 2 or more consecutive
  54.    newlines in the input file (i.e., one or more blank lines). */
  55. #define DEFAULT_PARAGRAPH_SPACING 1
  56.  
  57. /* **************************************************************** */
  58. /*                                    */
  59. /*            Include File Declarations               */
  60. /*                                    */
  61. /* **************************************************************** */
  62.  
  63. #if defined (_AIX)
  64. #  pragma alloca
  65. #endif /* _AIX */
  66.  
  67. #include <stdio.h>
  68. #include <sys/types.h>
  69. #include <ctype.h>
  70. #include <sys/stat.h>
  71. #include <ctype.h>
  72. #include <pwd.h>
  73. #include <errno.h>
  74. #include "getopt.h"
  75.  
  76. #if defined (VMS)
  77. #include <perror.h>
  78. #endif
  79.  
  80. #if defined (USG) || defined (VMS)
  81. #include <string.h>
  82. #include <time.h>
  83. #else
  84. #include <strings.h>
  85. #include <sys/time.h>
  86. #endif
  87.  
  88. #include <fcntl.h>
  89. #include <sys/file.h>
  90.  
  91. #if defined (USG) || defined (hpux)
  92. #define bcopy(source, dest, count) memcpy (dest, source, count)
  93. char *index(s,c) char *s; { char *strchr(); return strchr(s,c); }
  94. char *rindex(s,c) char *s; { char *strrchr(); return strrchr(s,c); }
  95. #endif /* USG || hpux */
  96.  
  97. #if defined (__GNUC__)
  98. #  define alloca __builtin_alloca
  99. #else
  100. #  if defined (sparc)
  101. #    include <alloca.h>
  102. #  else
  103. #    if !defined (_AIX)
  104. extern char *alloca ();
  105. #    endif /* _AIX */
  106. #  endif /* !sparc */
  107. #endif /* !__GNUC__ */
  108.  
  109. /* Forward declarations. */
  110. void *xmalloc (), *xrealloc ();
  111.  
  112. /* Non-zero means that we are currently hacking the insides of an
  113.    insertion which would use a fixed width font. */
  114. static int in_fixed_width_font = 0;
  115.  
  116. /* Non-zero means that start_paragraph () MUST be called before we pay
  117.    any attention to close_paragraph () calls. */
  118. int must_start_paragraph = 0;
  119.  
  120. /* Under DOS or OS/2, we can't use stat so we must count the file size
  121.    to know when things should be split.
  122. */
  123. int current_file_size = 0;
  124.  
  125. /* Some systems don't declare this function in pwd.h. */
  126. struct passwd *getpwnam ();
  127.  
  128.  
  129. /* **************************************************************** */
  130. /*                                    */
  131. /*                  Global Defines                  */
  132. /*                                    */
  133. /* **************************************************************** */
  134.  
  135. /* Error levels */
  136. #define NO_ERROR 0
  137. #define SYNTAX     2
  138. #define FATAL     4
  139.  
  140. /* How to allocate permanent storage for STRING. */
  141. #define savestring(x) \
  142.   ((char *)strcpy ((char *)xmalloc (1 + ((x) ? strlen (x) : 0)), \
  143.            (x) ? (x) : ""))
  144.  
  145. /* C's standard macros don't check to make sure that the characters being
  146.    changed are within range.  So I have to check explicitly. */
  147.  
  148. /* GNU Library doesn't have toupper().  Until GNU gets this fixed, I will
  149.    have to do it. */
  150. #ifndef toupper
  151. #define toupper(c) ((c) - 32)
  152. #endif
  153.  
  154. #define coerce_to_upper(c) ((islower(c) ? toupper(c) : (c)))
  155. #define coerce_to_lower(c) ((isupper(c) ? tolower(c) : (c)))
  156.  
  157. #define control_character_bit 0x40 /* %01000000, must be off. */
  158. #define meta_character_bit 0x080/* %10000000, must be on.  */
  159. #define CTL(c) ((c) & (~control_character_bit))
  160. #define UNCTL(c) coerce_to_upper(((c)|control_character_bit))
  161. #define META(c) ((c) | (meta_character_bit))
  162. #define UNMETA(c) ((c) & (~meta_character_bit))
  163.  
  164. #define whitespace(c) (((c) == '\t') || ((c) == ' '))
  165. #define sentence_ender(c) ((c) == '.' || (c) == '?' || (c) == '!')
  166. #define cr_or_whitespace(c) (((c) == '\t') || ((c) == ' ') || ((c) == '\n'))
  167.  
  168. #ifndef isletter
  169. #define isletter(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
  170. #endif
  171.  
  172. #ifndef isupper
  173. #define isupper(c) ((c) >= 'A' && (c) <= 'Z')
  174. #endif
  175.  
  176. #ifndef isdigit
  177. #define isdigit(c)  ((c) >= '0' && (c) <= '9')
  178. #endif
  179.  
  180. #ifndef digit_value
  181. #define digit_value(c) ((c) - '0')
  182. #endif
  183.  
  184. #define member(c, s) (index (s, c) != NULL)
  185.  
  186. #define COMMAND_PREFIX '@'
  187.  
  188. /* Stuff for splitting large files. */
  189. #define SPLIT_SIZE_THRESHOLD 70000    /* What's good enough for Stallman... */
  190. #define DEFAULT_SPLIT_SIZE 50000/* Is probably good enough for me. */
  191. int splitting = 1;        /* Always true for now. */
  192.  
  193. typedef int FUNCTION ();    /* So I can say FUNCTION *foo; */
  194.  
  195.  
  196. /* **************************************************************** */
  197. /*                                    */
  198. /*                Global Variables                */
  199. /*                                    */
  200. /* **************************************************************** */
  201.  
  202. /* Global pointer to argv[0]. */
  203. char *progname;
  204.  
  205. /* The current input file state. */
  206. char *input_filename;
  207. char *input_text;
  208. int size_of_input_text;
  209. int input_text_offset;
  210. int line_number;
  211.  
  212. #define curchar() input_text[input_text_offset]
  213.  
  214. #define command_char(c) ((!whitespace(c)) && \
  215.              ((c) != '\n') && \
  216.              ((c) != '{') && \
  217.              ((c) != '}'))
  218.  
  219. #define skip_whitespace() while (input_text_offset != size_of_input_text \
  220.                  && whitespace(curchar()))\
  221.   input_text_offset++
  222.  
  223. /* Return non-zero if STRING is the text at input_text + input_text_offset,
  224.    else zero. */
  225. #define looking_at(string) \
  226.   (strncmp (input_text + input_text_offset, string, strlen (string)) == 0)
  227.  
  228. /* And writing to the output. */
  229.  
  230. /* The output file name. */
  231. char *output_filename = (char *)NULL;
  232. char *pretty_output_filename;
  233.  
  234. /* Name of the output file that the user elected to pass on the command line.
  235.    Such a name overrides any name found with the @setfilename command. */
  236. char *command_output_filename = (char *)NULL;
  237.  
  238. /* Current output stream. */
  239. FILE *output_stream;
  240.  
  241. /* Position in the output file. */
  242. int output_position;
  243.  
  244. /* Output paragraph buffer. */
  245. unsigned char *output_paragraph;
  246.  
  247. /* Offset into OUTPUT_PARAGRAPH. */
  248. int output_paragraph_offset;
  249.  
  250. /* The output paragraph "cursor" horizontal position. */
  251. int output_column = 0;
  252.  
  253. /* Non-zero means output_paragraph contains text. */
  254. int paragraph_is_open = 0;
  255.  
  256. #define INITIAL_PARAGRAPH_SPACE 5000
  257. int paragraph_buffer_len = INITIAL_PARAGRAPH_SPACE;
  258.  
  259. /* Filling.. */
  260. /* Non-zero indicates that filling will take place on long lines. */
  261. int filling_enabled = 1;
  262.  
  263. /* Non-zero means that words are not to be split, even in long lines.  This
  264.    gets changed for cm_w (). */
  265. int non_splitting_words = 0;
  266.  
  267. /* Non-zero indicates that filling a line also indents the new line. */
  268. int indented_fill = 0;
  269.  
  270. /* The column at which long lines are broken. */
  271. int fill_column = 72;
  272.  
  273. /* The amount of indentation to apply at the start of each line. */
  274. int current_indent = 0;
  275.  
  276. /* The amount of indentation to add at the starts of paragraphs.
  277.    0 means don't change existing indentation at paragraph starts.
  278.    > 0 is amount to indent new paragraphs by.
  279.    < 0 means indent to column zero by removing indentation if necessary.
  280.  
  281.    This is normally zero, but some people prefer paragraph starts to be
  282.    somewhat more indented than paragraph bodies.  A pretty value for
  283.    this is 3. */
  284. int paragraph_start_indent = PARAGRAPH_START_INDENT;
  285.  
  286. /* Non-zero means that the use of paragraph_start_indent is inhibited.
  287.    @example uses this to line up the left columns of the example text.
  288.    A negative value for this variable is incremented each time it is used.
  289.    @noindent uses this to inhibit indentation for a single paragraph.  */
  290. int inhibit_paragraph_indentation = 0;
  291.  
  292. /* Indentation that is pending insertion.  We have this for hacking lines
  293.    which look blank, but contain whitespace.  We want to treat those as
  294.    blank lines. */
  295. int pending_indent = 0;
  296.  
  297. /* The amount that indentation increases/decreases by. */
  298. int default_indentation_increment = DEFAULT_INDENTATION_INCREMENT;
  299.  
  300. /* Non-zero indicates that indentation is temporarily turned off. */
  301. int no_indent = 1;
  302.  
  303. /* Non-zero means forcing output text to be flushright. */
  304. int force_flush_right = 0;
  305.  
  306. /* Non-zero means that the footnote style for this document was set on
  307.    the command line, which overrides any other settings. */
  308. int footnote_style_preset = 0;
  309.  
  310. /* Non-zero means that we automatically number footnotes that have no
  311.    specified marker. */
  312. int number_footnotes = 1;
  313.  
  314. /* The current footnote number in this node.  Each time a new node is
  315.    started this is reset to 1. */
  316. int current_footnote_number = 1;
  317.  
  318. /* Command name in the process of being hacked. */
  319. char *command;
  320.  
  321. /* The index in our internal command table of the currently
  322.    executing command. */
  323. int command_index;
  324.  
  325. /* A stack of file information records.  If a new file is read in with
  326.    "@input", we remember the old input file state on this stack. */
  327. typedef struct fstack
  328. {
  329.   struct fstack *next;
  330.   char *filename;
  331.   char *text;
  332.   int size;
  333.   int offset;
  334.   int line_number;
  335. } FSTACK;
  336.  
  337. FSTACK *filestack = (FSTACK *) NULL;
  338.  
  339. /* Stuff for nodes. */
  340. /* The current nodes node name. */
  341. char *current_node = (char *)NULL;
  342.  
  343. /* The current nodes section level. */
  344. int current_section = 0;
  345.  
  346. /* The filename of the current input file.  This is never freed. */
  347. char *node_filename = (char *)NULL;
  348.  
  349. /* What we remember for each node. */
  350. typedef struct tentry
  351. {
  352.   struct tentry *next_ent;
  353.   char *node;        /* name of this node. */
  354.   char *prev;        /* name of "Prev:" for this node. */
  355.   char *next;        /* name of "Next:" for this node. */
  356.   char *up;        /* name of "Up:" for this node.   */
  357.   int position;        /* output file position of this node. */
  358.   int line_no;        /* defining line in source file. */
  359.   char *filename;    /* The file that this node was found in. */
  360.   int touched;        /* non-zero means this node has been referenced. */
  361.   int flags;        /* Room for growth.  Right now, contains 1 bit. */
  362. } TAG_ENTRY;
  363.  
  364. /* If node-a has a "Next" for node-b, but node-b has no "Prev" for node-a,
  365.    we turn on this flag bit in node-b's tag entry.  This means that when
  366.    it is time to validate node-b, we don't report an additional error
  367.    if there was no "Prev" field. */
  368. #define PREV_ERROR 0x1
  369. #define NEXT_ERROR 0x2
  370. #define UP_ERROR   0x4
  371. #define NO_WARN       0x8
  372. #define IS_TOP        0x10
  373.  
  374. TAG_ENTRY *tag_table = (TAG_ENTRY *) NULL;
  375.  
  376. #define HAVE_MACROS
  377. #if defined (HAVE_MACROS)
  378. /* Macro definitions for user-defined commands. */
  379. typedef struct {
  380.   char *name;            /* Name of the macro. */
  381.   char *definition;        /* Definition text. */
  382.   char *filename;        /* File where this macro is defined. */
  383.   int lineno;            /* Line number within FILENAME. */
  384. } MACRO_DEF;
  385.  
  386. void add_macro (), execute_macro ();
  387. MACRO_DEF *find_macro (), *delete_macro ();
  388. #endif /* HAVE_MACROS */
  389.  
  390. /* Menu reference, *note reference, and validation hacking. */
  391.  
  392. /* The various references that we know about. */
  393. enum reftype
  394. {
  395.   menu_reference, followed_reference
  396. };
  397.  
  398. /* A structure to remember references with.  A reference to a node is
  399.    either an entry in a menu, or a cross-reference made with [px]ref. */
  400. typedef struct node_ref
  401. {
  402.   struct node_ref *next;
  403.   char *node;            /* Name of node referred to. */
  404.   char *containing_node;    /* Name of node containing this reference. */
  405.   int line_no;            /* Line number where the reference occurs. */
  406.   int section;            /* Section level where the reference occurs. */
  407.   char *filename;        /* Name of file where the reference occurs. */
  408.   enum reftype type;        /* Type of reference, either menu or note. */
  409. } NODE_REF;
  410.  
  411. /* The linked list of such structures. */
  412. NODE_REF *node_references = (NODE_REF *) NULL;
  413.  
  414. /* Flag which tells us whether to examine menu lines or not. */
  415. int in_menu = 0;
  416.  
  417. /* Flags controlling the operation of the program. */
  418.  
  419. /* Default is to notify users of bad choices. */
  420. int print_warnings = 1;
  421.  
  422. /* Default is to check node references. */
  423. int validating = 1;
  424.  
  425. /* Non-zero means do not output "Node: Foo" for node separations. */
  426. int no_headers = 0;
  427.  
  428. /* Number of errors that we tolerate on a given fileset. */
  429. int max_error_level = 100;
  430.  
  431. /* Maximum number of references to a single node before complaining. */
  432. int reference_warning_limit = 1000;
  433.  
  434. /* Non-zero means print out information about what is going on when it
  435.    is going on. */
  436. int verbose_mode = 1;
  437.  
  438. /* The list of commands that we hack in texinfo.  Each one
  439.    has an associated function.  When the command is encountered in the
  440.    text, the associated function is called with START as the argument.
  441.    If the function expects arguments in braces, it remembers itself on
  442.    the stack.  When the corresponding close brace is encountered, the
  443.    function is called with END as the argument. */
  444.  
  445. #define START 0
  446. #define END 1
  447.  
  448. typedef struct brace_element
  449. {
  450.   struct brace_element *next;
  451.   FUNCTION *proc;
  452.   int pos, line;
  453. } BRACE_ELEMENT;
  454.  
  455. BRACE_ELEMENT *brace_stack = (BRACE_ELEMENT *) NULL;
  456.  
  457. /* Forward declarations. */
  458.  
  459. int
  460. insert_self (), cm_tex (), cm_asterisk (), cm_dots (), cm_bullet (),
  461. cm_TeX (), cm_copyright (), cm_code (), cm_samp (), cm_file (), cm_kbd (),
  462. cm_key (), cm_ctrl (), cm_var (), cm_dfn (), cm_emph (), cm_strong (),
  463. cm_cite (), cm_italic (), cm_bold (), cm_roman (), cm_title (), cm_w (),
  464. cm_refill (), cm_titlefont ();
  465.  
  466. int
  467. cm_chapter (), cm_unnumbered (), cm_appendix (), cm_top (),
  468. cm_section (), cm_unnumberedsec (), cm_appendixsec (),
  469. cm_subsection (), cm_unnumberedsubsec (), cm_appendixsubsec (),
  470. cm_subsubsection (), cm_unnumberedsubsubsec (), cm_appendixsubsubsec (),
  471. cm_heading (), cm_chapheading (), cm_subheading (), cm_subsubheading (),
  472. cm_majorheading ();
  473.  
  474. /* All @defxxx commands map to cm_defun (). */
  475. int cm_defun ();
  476.  
  477. int
  478.   cm_node (), cm_menu (), cm_xref (), cm_ftable (), cm_vtable (), cm_pxref (),
  479.   cm_inforef (), cm_quotation (), cm_display (), cm_itemize (),
  480.   cm_enumerate (), cm_table (), cm_itemx (), cm_noindent (),
  481.   cm_setfilename (), cm_comment (), cm_br (), cm_sp (), cm_page (),
  482.   cm_group (), cm_need (), cm_center (), cm_include (), cm_bye (),
  483.   cm_item (), cm_end (), cm_infoinclude (), cm_ifinfo (), cm_kindex (),
  484.   cm_cindex (), cm_findex (), cm_pindex (), cm_vindex (),
  485.   cm_tindex (), cm_asis (), cm_synindex (), cm_settitle (),
  486.   cm_setchapternewpage (), cm_printindex (), cm_minus (), cm_footnote (),
  487.   cm_force_abbreviated_whitespace (), cm_example (), cm_smallexample (),
  488.   cm_lisp (), cm_format (), cm_exdent (), cm_defindex (), cm_defcodeindex (),
  489.   cm_sc (), cm_result (), cm_expansion (), cm_equiv (), cm_print (),
  490.   cm_error (), cm_point (), cm_smallbook (), cm_headings (), cm_today (),
  491.   cm_flushleft (),  cm_flushright (), cm_smalllisp (), cm_finalout (),
  492.   cm_math (), cm_cartouche (), cm_ignore_sentence_ender ();
  493.  
  494. /* Conditionals. */
  495. int cm_set (), cm_clear (), cm_ifset (), cm_ifclear ();
  496.  
  497. #if defined (HAVE_MACROS)
  498. /* Define a user-defined command which is simple substitution. */
  499. int cm_macro (), cm_unmacro ();
  500. #endif /* HAVE_MACROS */
  501.  
  502. /* Options. */
  503. int cm_paragraphindent (), cm_footnotestyle ();
  504.  
  505. /* Internals. */
  506. int do_nothing (), command_name_condition ();
  507. int misplaced_brace (), cm_obsolete ();
  508.  
  509. typedef struct
  510. {
  511.   char *name;
  512.   FUNCTION *proc;
  513.   int argument_in_braces;
  514. } COMMAND;
  515.  
  516. /* Stuff for defining commands on the fly. */
  517. COMMAND **user_command_array = (COMMAND **) NULL;
  518. int user_command_array_len = 0;
  519.  
  520. #define NO_BRACE_ARGS 0
  521. #define BRACE_ARGS 1
  522.  
  523. static COMMAND CommandTable[] = {
  524.   { "!", cm_ignore_sentence_ender, NO_BRACE_ARGS },
  525.   { "'", insert_self, NO_BRACE_ARGS },
  526.   { "*", cm_asterisk, NO_BRACE_ARGS },
  527.   { ".", cm_ignore_sentence_ender, NO_BRACE_ARGS },
  528.   { ":", cm_force_abbreviated_whitespace, NO_BRACE_ARGS },
  529.   { "?", cm_ignore_sentence_ender, NO_BRACE_ARGS },
  530.   { "@", insert_self, NO_BRACE_ARGS },
  531.   { " ", insert_self, NO_BRACE_ARGS },
  532.   { "\n", insert_self, NO_BRACE_ARGS },
  533.   { "TeX", cm_TeX, BRACE_ARGS },
  534.   { "`", insert_self, NO_BRACE_ARGS },
  535.   { "appendix", cm_appendix, NO_BRACE_ARGS },
  536.   { "appendixsection", cm_appendixsec, NO_BRACE_ARGS },
  537.   { "appendixsec", cm_appendixsec, NO_BRACE_ARGS },
  538.   { "appendixsubsec", cm_appendixsubsec, NO_BRACE_ARGS },
  539.   { "appendixsubsubsec", cm_appendixsubsubsec, NO_BRACE_ARGS },
  540.   { "asis", cm_asis, BRACE_ARGS },
  541.   { "b", cm_bold, BRACE_ARGS },
  542.   { "br", cm_br, NO_BRACE_ARGS },
  543.   { "bullet", cm_bullet, BRACE_ARGS },
  544.   { "bye", cm_bye, NO_BRACE_ARGS },
  545.   { "c", cm_comment, NO_BRACE_ARGS },
  546.   { "cartouche", cm_cartouche, NO_BRACE_ARGS },
  547.   { "center", cm_center, NO_BRACE_ARGS },
  548.   { "chapheading", cm_chapheading, NO_BRACE_ARGS },
  549.   { "chapter", cm_chapter, NO_BRACE_ARGS },
  550.   { "cindex", cm_cindex, NO_BRACE_ARGS },
  551.   { "cite", cm_cite, BRACE_ARGS },
  552.   { "clear", cm_clear, NO_BRACE_ARGS },
  553.   { "code", cm_code, BRACE_ARGS },
  554.   { "comment", cm_comment, NO_BRACE_ARGS },
  555.   { "contents", do_nothing, NO_BRACE_ARGS },
  556.   { "copyright", cm_copyright, BRACE_ARGS },
  557.   { "ctrl", cm_ctrl, BRACE_ARGS },
  558.   { "defcodeindex", cm_defcodeindex, NO_BRACE_ARGS },
  559.   { "defindex", cm_defindex, NO_BRACE_ARGS },
  560.   { "dfn", cm_dfn, BRACE_ARGS },
  561.  
  562. /* The `def' commands. */
  563.   { "deffn", cm_defun, NO_BRACE_ARGS },
  564.   { "deffnx", cm_defun, NO_BRACE_ARGS },
  565.   { "defun", cm_defun, NO_BRACE_ARGS },
  566.   { "defunx", cm_defun, NO_BRACE_ARGS },
  567.   { "defmac", cm_defun, NO_BRACE_ARGS },
  568.   { "defmacx", cm_defun, NO_BRACE_ARGS },
  569.   { "defspec", cm_defun, NO_BRACE_ARGS },
  570.   { "defspecx", cm_defun, NO_BRACE_ARGS },
  571.   { "defvr", cm_defun, NO_BRACE_ARGS },
  572.   { "defvrx", cm_defun, NO_BRACE_ARGS },
  573.   { "defvar", cm_defun, NO_BRACE_ARGS },
  574.   { "defvarx", cm_defun, NO_BRACE_ARGS },
  575.   { "defopt", cm_defun, NO_BRACE_ARGS },
  576.   { "defoptx", cm_defun, NO_BRACE_ARGS },
  577.   { "deftypefn", cm_defun, NO_BRACE_ARGS },
  578.   { "deftypefnx", cm_defun, NO_BRACE_ARGS },
  579.   { "deftypefun", cm_defun, NO_BRACE_ARGS },
  580.   { "deftypefunx", cm_defun, NO_BRACE_ARGS },
  581.   { "deftypevr", cm_defun, NO_BRACE_ARGS },
  582.   { "deftypevrx", cm_defun, NO_BRACE_ARGS },
  583.   { "deftypevar", cm_defun, NO_BRACE_ARGS },
  584.   { "deftypevarx", cm_defun, NO_BRACE_ARGS },
  585.   { "defcv", cm_defun, NO_BRACE_ARGS },
  586.   { "defcvx", cm_defun, NO_BRACE_ARGS },
  587.   { "defivar", cm_defun, NO_BRACE_ARGS },
  588.   { "defivarx", cm_defun, NO_BRACE_ARGS },
  589.   { "defop", cm_defun, NO_BRACE_ARGS },
  590.   { "defopx", cm_defun, NO_BRACE_ARGS },
  591.   { "defmethod", cm_defun, NO_BRACE_ARGS },
  592.   { "defmethodx", cm_defun, NO_BRACE_ARGS },
  593.   { "deftypemethod", cm_defun, NO_BRACE_ARGS },
  594.   { "deftypemethodx", cm_defun, NO_BRACE_ARGS },
  595.   { "deftp", cm_defun, NO_BRACE_ARGS },
  596.   { "deftpx", cm_defun, NO_BRACE_ARGS },
  597. /* The end of the `def' commands. */
  598.  
  599.   { "display", cm_display, NO_BRACE_ARGS },
  600.   { "dots", cm_dots, BRACE_ARGS },
  601.   { "dmn", do_nothing, BRACE_ARGS },
  602.   { "emph", cm_emph, BRACE_ARGS },
  603.   { "end", cm_end, NO_BRACE_ARGS },
  604.   { "enumerate", cm_enumerate, NO_BRACE_ARGS },
  605.   { "equiv", cm_equiv, BRACE_ARGS },
  606.   { "error", cm_error, BRACE_ARGS },
  607.   { "example", cm_example, NO_BRACE_ARGS },
  608.   { "exdent", cm_exdent, NO_BRACE_ARGS },
  609.   { "expansion", cm_expansion, BRACE_ARGS },
  610.   { "file", cm_file, BRACE_ARGS },
  611.   { "findex", cm_findex, NO_BRACE_ARGS },
  612.   { "finalout", do_nothing, NO_BRACE_ARGS },
  613.   { "flushleft", cm_flushleft, NO_BRACE_ARGS },
  614.   { "flushright", cm_flushright, NO_BRACE_ARGS },
  615.   { "format", cm_format, NO_BRACE_ARGS },
  616.   { "ftable", cm_ftable, NO_BRACE_ARGS },
  617.   { "group", cm_group, NO_BRACE_ARGS },
  618.   { "heading", cm_heading, NO_BRACE_ARGS },
  619.   { "headings", cm_headings, NO_BRACE_ARGS },
  620.   { "i", cm_italic, BRACE_ARGS },
  621.   { "iappendix", cm_appendix, NO_BRACE_ARGS },
  622.   { "iappendixsection", cm_appendixsec, NO_BRACE_ARGS },
  623.   { "iappendixsec", cm_appendixsec, NO_BRACE_ARGS },
  624.   { "iappendixsubsec", cm_appendixsubsec, NO_BRACE_ARGS },
  625.   { "iappendixsubsubsec", cm_appendixsubsubsec, NO_BRACE_ARGS },
  626.   { "ichapter", cm_chapter, NO_BRACE_ARGS },
  627.   { "ifclear", cm_ifclear, NO_BRACE_ARGS },
  628.   { "ifinfo", cm_ifinfo, NO_BRACE_ARGS },
  629.   { "ifset", cm_ifset, NO_BRACE_ARGS },
  630.   { "iftex", command_name_condition, NO_BRACE_ARGS },
  631.   { "ignore", command_name_condition, NO_BRACE_ARGS },
  632.   { "include", cm_include, NO_BRACE_ARGS },
  633.   { "inforef", cm_inforef, BRACE_ARGS },
  634.   { "input", cm_include, NO_BRACE_ARGS },
  635.   { "isection", cm_section, NO_BRACE_ARGS },
  636.   { "isubsection", cm_subsection, NO_BRACE_ARGS },
  637.   { "isubsubsection", cm_subsubsection, NO_BRACE_ARGS },
  638.   { "item", cm_item, NO_BRACE_ARGS },
  639.   { "itemize", cm_itemize, NO_BRACE_ARGS },
  640.   { "itemx", cm_itemx, NO_BRACE_ARGS },
  641.   { "iunnumbered", cm_unnumbered, NO_BRACE_ARGS },
  642.   { "iunnumberedsec", cm_unnumberedsec, NO_BRACE_ARGS },
  643.   { "iunnumberedsubsec", cm_unnumberedsubsec, NO_BRACE_ARGS },
  644.   { "iunnumberedsubsubsec", cm_unnumberedsubsubsec, NO_BRACE_ARGS },
  645.   { "kbd", cm_kbd, BRACE_ARGS },
  646.   { "key", cm_key, BRACE_ARGS },
  647.   { "kindex", cm_kindex, NO_BRACE_ARGS },
  648.   { "lisp", cm_lisp, NO_BRACE_ARGS },
  649.   { "macro", cm_macro, NO_BRACE_ARGS },
  650.   { "majorheading", cm_majorheading, NO_BRACE_ARGS },
  651.   { "math", cm_math, BRACE_ARGS },
  652.   { "menu", cm_menu, NO_BRACE_ARGS },
  653.   { "minus", cm_minus, BRACE_ARGS },
  654.   { "need", cm_need, NO_BRACE_ARGS },
  655.   { "node", cm_node, NO_BRACE_ARGS },
  656.   { "noindent", cm_noindent, NO_BRACE_ARGS },
  657.   { "nwnode", cm_node, NO_BRACE_ARGS },
  658.   { "page", do_nothing, NO_BRACE_ARGS },
  659.   { "pindex", cm_pindex, NO_BRACE_ARGS },
  660.   { "point", cm_point, BRACE_ARGS },
  661.   { "print", cm_print, BRACE_ARGS },
  662.   { "printindex", cm_printindex, NO_BRACE_ARGS },
  663.   { "pxref", cm_pxref, BRACE_ARGS },
  664.   { "quotation", cm_quotation, NO_BRACE_ARGS },
  665.   { "r", cm_roman, BRACE_ARGS },
  666.   { "ref", cm_xref, BRACE_ARGS },
  667.   { "refill", cm_refill, NO_BRACE_ARGS },
  668.   { "result", cm_result, BRACE_ARGS },
  669.   { "samp", cm_samp, BRACE_ARGS },
  670.   { "sc", cm_sc, BRACE_ARGS },
  671.   { "section", cm_section, NO_BRACE_ARGS },
  672.   { "set", cm_set, NO_BRACE_ARGS },
  673.   { "setchapternewpage", cm_setchapternewpage, NO_BRACE_ARGS },
  674.   { "setfilename", cm_setfilename, NO_BRACE_ARGS },
  675.   { "settitle", cm_settitle, NO_BRACE_ARGS },
  676.   { "shortcontents", do_nothing, NO_BRACE_ARGS },
  677.   { "smallbook", cm_smallbook, NO_BRACE_ARGS },
  678.   { "smallexample", cm_smallexample, NO_BRACE_ARGS },
  679.   { "smalllisp", cm_smalllisp, NO_BRACE_ARGS },
  680.   { "sp", cm_sp, NO_BRACE_ARGS },
  681.   { "strong", cm_strong, BRACE_ARGS },
  682.   { "subheading", cm_subheading, NO_BRACE_ARGS },
  683.   { "subsection", cm_subsection, NO_BRACE_ARGS },
  684.   { "subsubheading", cm_subsubheading, NO_BRACE_ARGS },
  685.   { "subsubsection", cm_subsubsection, NO_BRACE_ARGS },
  686.   { "summarycontents", do_nothing, NO_BRACE_ARGS },
  687.   { "syncodeindex", cm_synindex, NO_BRACE_ARGS },
  688.   { "synindex", cm_synindex, NO_BRACE_ARGS },
  689.   { "t", cm_title, BRACE_ARGS },
  690.   { "table", cm_table, NO_BRACE_ARGS },
  691.   { "tex", command_name_condition, NO_BRACE_ARGS },
  692.   { "tindex", cm_tindex, NO_BRACE_ARGS },
  693.   { "titlefont", cm_titlefont, BRACE_ARGS },
  694.   { "titlepage", command_name_condition, NO_BRACE_ARGS },
  695.   { "titlespec", command_name_condition, NO_BRACE_ARGS },
  696.   { "today", cm_today, BRACE_ARGS },
  697.   { "top", cm_top, NO_BRACE_ARGS  },
  698.   { "unmacro", cm_unmacro, NO_BRACE_ARGS },
  699.   { "unnumbered", cm_unnumbered, NO_BRACE_ARGS },
  700.   { "unnumberedsec", cm_unnumberedsec, NO_BRACE_ARGS },
  701.   { "unnumberedsubsec", cm_unnumberedsubsec, NO_BRACE_ARGS },
  702.   { "unnumberedsubsubsec", cm_unnumberedsubsubsec, NO_BRACE_ARGS },
  703.   { "var", cm_var, BRACE_ARGS },
  704.   { "vindex", cm_vindex, NO_BRACE_ARGS },
  705.   { "vtable", cm_vtable, NO_BRACE_ARGS },
  706.   { "w", cm_w, BRACE_ARGS },
  707.   { "xref", cm_xref, BRACE_ARGS },
  708.   { "{", insert_self, NO_BRACE_ARGS },
  709.   { "}", insert_self, NO_BRACE_ARGS },
  710.  
  711.   /* Some obsoleted commands. */
  712.   { "infotop", cm_obsolete, NO_BRACE_ARGS },
  713.   { "infounnumbered", cm_obsolete, NO_BRACE_ARGS },
  714.   { "infounnumberedsec", cm_obsolete, NO_BRACE_ARGS },
  715.   { "infounnumberedsubsec", cm_obsolete, NO_BRACE_ARGS },
  716.   { "infounnumberedsubsubsec", cm_obsolete, NO_BRACE_ARGS },
  717.   { "infoappendix", cm_obsolete, NO_BRACE_ARGS },
  718.   { "infoappendixsec", cm_obsolete, NO_BRACE_ARGS },
  719.   { "infoappendixsubsec", cm_obsolete, NO_BRACE_ARGS },
  720.   { "infoappendixsubsubsec", cm_obsolete, NO_BRACE_ARGS },
  721.   { "infochapter", cm_obsolete, NO_BRACE_ARGS },
  722.   { "infosection", cm_obsolete, NO_BRACE_ARGS },
  723.   { "infosubsection", cm_obsolete, NO_BRACE_ARGS },
  724.   { "infosubsubsection", cm_obsolete, NO_BRACE_ARGS },
  725.  
  726.   /* Now @include does what this was supposed to. */
  727.   { "infoinclude", cm_infoinclude, NO_BRACE_ARGS },
  728.   { "footnote", cm_footnote, NO_BRACE_ARGS}, /* self-arg eater */
  729.   { "footnotestyle", cm_footnotestyle, NO_BRACE_ARGS },
  730.   { "paragraphindent", cm_paragraphindent, NO_BRACE_ARGS },
  731.  
  732.   {(char *) NULL, (FUNCTION *) NULL}, NO_BRACE_ARGS};
  733.  
  734. int major_version = 1;
  735. int minor_version = 43;
  736.  
  737. struct option long_options[] =
  738. {
  739.   { "error-limit", 1, 0, 'e' },            /* formerly -el */
  740.   { "fill-column", 1, 0, 'f' },            /* formerly -fc */
  741.   { "footnote-style", 1, 0, 's' },        /* formerly -ft */
  742.   { "no-headers", 0, &no_headers, 1 },        /* Do not output Node: foo */
  743.   { "no-pointer-validate", 0, &validating, 0 }, /* formerly -nv */
  744.   { "no-split", 0, &splitting, 0 },        /* formerly -ns */
  745.   { "no-validate", 0, &validating, 0 },        /* formerly -nv */
  746.   { "no-warn", 0, &print_warnings, 0 },        /* formerly -nw */
  747.   { "number-footnotes", 0, &number_footnotes, 1 },
  748.   { "no-number-footnotes", 0, &number_footnotes, 0 },
  749.   { "output", 1, 0, 'o' },
  750.   { "paragraph-indent", 1, 0, 'p' },        /* formerly -pi */
  751.   { "reference-limit", 1, 0, 'r' },        /* formerly -rl */
  752.   { "verbose", 0, &verbose_mode, 1 },        /* formerly -verbose */
  753.   { "version", 0, 0, 'V' },
  754.   {NULL, 0, NULL, 0}
  755. };
  756.  
  757. /* **************************************************************** */
  758. /*                                    */
  759. /*            Main ()  Start of code              */
  760. /*                                        */
  761. /* **************************************************************** */
  762.  
  763. /* For each file mentioned in the command line, process it, turning
  764.    texinfo commands into wonderfully formatted output text. */
  765. main (argc, argv)
  766.      int argc;
  767.      char **argv;
  768. {
  769.   extern int errors_printed;
  770.   char *filename_part ();
  771.   int c, ind;
  772.  
  773.   /* The name of this program is the last filename in argv[0]. */
  774.   progname = filename_part (argv[0]);
  775.  
  776.   /* Parse argument flags from the input line. */
  777.   while ((c = getopt_long (argc, argv, "f:o:p:e:r:s:V", long_options, &ind))
  778.      != EOF)
  779.     {
  780.       if (c == 0 && long_options[ind].flag == 0)
  781.     c = long_options[ind].val;
  782.  
  783.       switch (c)
  784.     {
  785.       /* User specified fill_column? */
  786.     case 'f':
  787.       if (sscanf (optarg, "%d", &fill_column) != 1)
  788.         usage ();
  789.       break;
  790.  
  791.       /* User specified output file? */
  792.     case 'o':
  793.       command_output_filename = savestring (optarg);
  794.       break;
  795.  
  796.       /* User specified paragraph indent (paragraph_start_index)? */
  797.     case 'p':
  798.       if (set_paragraph_indent (optarg) < 0)
  799.         usage ();
  800.       break;
  801.  
  802.       /* User specified error level? */
  803.     case 'e':
  804.       if (sscanf (optarg, "%d", &max_error_level) != 1)
  805.         usage ();
  806.       break;
  807.  
  808.       /* User specified reference warning limit? */
  809.     case 'r':
  810.       if (sscanf (optarg, "%d", &reference_warning_limit) != 1)
  811.         usage ();
  812.       break;
  813.  
  814.       /* User specified footnote style? */
  815.     case 's':
  816.       if (set_footnote_style (optarg) < 0)
  817.         usage ();
  818.       footnote_style_preset = 1;
  819.       break;
  820.  
  821.       /* User requested version info? */
  822.     case 'V':
  823.       print_version_info ();
  824.       exit (NO_ERROR);
  825.       break;
  826.  
  827.     case '?':
  828.       usage ();
  829.     }
  830.     }
  831.  
  832.   if (optind == argc)
  833.     usage ();
  834.   else if (verbose_mode)
  835.     print_version_info ();
  836.  
  837.   /* Remaining arguments are file names of texinfo files.
  838.      Convert them, one by one. */
  839.   while (optind != argc)
  840.     convert (argv[optind++]);
  841.  
  842.   if (errors_printed)
  843.     exit (SYNTAX);
  844.   else
  845.     exit (NO_ERROR);
  846. }
  847.  
  848. /* Display the version info of this invocation of Makeinfo. */
  849. print_version_info ()
  850. {
  851.   fprintf (stderr, "This is GNU Makeinfo version %d.%d.\n",
  852.        major_version, minor_version);
  853. }
  854.  
  855. /* **************************************************************** */
  856. /*                                    */
  857. /*            Generic Utilities                */
  858. /*                                    */
  859. /* **************************************************************** */
  860.  
  861. /* Just like malloc, but kills the program in case of fatal error. */
  862. void *
  863. xmalloc (nbytes)
  864.      int nbytes;
  865. {
  866.   void *temp = (void *) malloc (nbytes);
  867.  
  868.   if (nbytes && temp == (void *)NULL)
  869.     memory_error ("xmalloc", nbytes);
  870.  
  871.   return (temp);
  872. }
  873.  
  874. /* Like realloc (), but barfs if there isn't enough memory. */
  875. void *
  876. xrealloc (pointer, nbytes)
  877.      void *pointer;
  878.      int nbytes;
  879. {
  880.   void *temp;
  881.  
  882.   if (!pointer)
  883.     temp = (void *)xmalloc (nbytes);
  884.   else
  885.     temp = (void *)realloc (pointer, nbytes);
  886.  
  887.   if (nbytes && !temp)
  888.     memory_error ("xrealloc", nbytes);
  889.  
  890.   return (temp);
  891. }
  892.  
  893. memory_error (callers_name, bytes_wanted)
  894.      char *callers_name;
  895.      int bytes_wanted;
  896. {
  897.   char printable_string[80];
  898.  
  899.   sprintf (printable_string,
  900.        "Virtual memory exhausted in %s ()!  Needed %d bytes.",
  901.        callers_name, bytes_wanted);
  902.  
  903.   error (printable_string);
  904.   abort ();
  905. }
  906.  
  907. /* Tell the user how to use this program. */
  908. usage ()
  909. {
  910.   fprintf (stderr, "Usage: %s [options] texinfo-file...\n\
  911. \n\
  912. This program accepts as input files of texinfo commands and text\n\
  913. and outputs a file suitable for reading with GNU Info.\n\
  914. \n\
  915. The options are:\n\
  916. `+no-validate' to suppress node cross reference validation.\n\
  917. `+no-warn' to suppress warning messages (errors are still output).\n\
  918. `+no-split' to suppress the splitting of large files.\n\
  919. `+no-headers' to suppress the output of Node: Foo headers.\n\
  920. `+verbose' to print information about what is being done.\n\
  921. `+version' to print the version number of Makeinfo.\n\
  922. `+paragraph-indent NUM' to set the paragraph indent to NUM (default %d).\n\
  923. `+fill-column NUM' to set the filling column to NUM (default %d).\n\
  924. `+error-limit NUM' to set the error limit to NUM (default %d).\n\
  925. `+reference-limit NUM' to set the reference warning limit to NUM (default %d).\n\
  926. `+footnote-style STYLE' to set the footnote style to STYLE.  STYLE should\n\
  927.   either be `separate' to place footnotes in their own node, or\n\
  928.   `end', to place the footnotes at the end of the node in which they are\n\
  929.    defined (the default).\n\n",
  930.        progname, paragraph_start_indent,
  931.        fill_column, max_error_level, reference_warning_limit);
  932.   exit (FATAL);
  933. }
  934.  
  935. /* **************************************************************** */
  936. /*                                    */
  937. /*            Manipulating Lists                  */
  938. /*                                        */
  939. /* **************************************************************** */
  940.  
  941. typedef struct generic_list
  942. {
  943.   struct generic_list *next;
  944. }            GENERIC_LIST;
  945.  
  946. /* Reverse the chain of structures in LIST.  Output the new head
  947.    of the chain.  You should always assign the output value of this
  948.    function to something, or you will lose the chain. */
  949. GENERIC_LIST *
  950. reverse_list (list)
  951.      register GENERIC_LIST *list;
  952. {
  953.   register GENERIC_LIST *next;
  954.   register GENERIC_LIST *prev = (GENERIC_LIST *) NULL;
  955.  
  956.   while (list)
  957.     {
  958.       next = list->next;
  959.       list->next = prev;
  960.       prev = list;
  961.       list = next;
  962.     }
  963.   return (prev);
  964. }
  965.  
  966.  
  967. /* **************************************************************** */
  968. /*                                    */
  969. /*            Pushing and Popping Files               */
  970. /*                                    */
  971. /* **************************************************************** */
  972.  
  973. /* Find and load the file named FILENAME.  Return a pointer to
  974.    the loaded file, or NULL if it can't be loaded. */
  975. char *
  976. find_and_load (filename)
  977.      char *filename;
  978. {
  979.   struct stat fileinfo;
  980.   int file, n, i, count = 0, got;
  981.   char *result = (char *) NULL;
  982.  
  983.   if (stat (filename, &fileinfo) != 0)
  984.     goto error_exit;
  985.  
  986.   file = open (filename, O_RDONLY|O_BINARY);
  987.   if (file < 0)
  988.     goto error_exit;
  989.  
  990.   /* Load the file. */
  991.   result = (char *)xmalloc (fileinfo.st_size);
  992.  
  993.   /* VMS stat lies about the st_size value.  The actual number of
  994.      readable bytes is always less than this value.  The arcane
  995.      mysteries of VMS/RMS are too much to probe, so this hack
  996.     suffices to make things work. */
  997. #if defined (VMS)
  998.   while ((n = read (file, result+count, fileinfo.st_size)) > 0)
  999.     count += n;
  1000.   if (n == -1)
  1001. #else
  1002.     count = fileinfo.st_size;
  1003.     got = read (file, result, fileinfo.st_size);
  1004.     if (got != fileinfo.st_size)
  1005. #endif
  1006.   error_exit:
  1007.     {
  1008.       if (result)
  1009.     free (result);
  1010.       if (file != -1)
  1011.     close (file);
  1012.       return ((char *) NULL);
  1013.     }
  1014.   close (file);
  1015.  
  1016.   /* Set the globals to the new file. */
  1017.   input_text = result;
  1018.   size_of_input_text = fileinfo.st_size;
  1019.   input_filename = savestring (filename);
  1020.   node_filename = savestring (filename);
  1021.   input_text_offset = 0;
  1022.   line_number = 1;
  1023.   return (result);
  1024. }
  1025.  
  1026. /* Save the state of the current input file. */
  1027. pushfile ()
  1028. {
  1029.   FSTACK *newstack = (FSTACK *) xmalloc (sizeof (FSTACK));
  1030.   newstack->filename = input_filename;
  1031.   newstack->text = input_text;
  1032.   newstack->size = size_of_input_text;
  1033.   newstack->offset = input_text_offset;
  1034.   newstack->line_number = line_number;
  1035.   newstack->next = filestack;
  1036.  
  1037.   filestack = newstack;
  1038.   push_node_filename ();
  1039. }
  1040.  
  1041. /* Make the current file globals be what is on top of the file stack. */
  1042. popfile ()
  1043. {
  1044.   extern int executing_string;
  1045.   FSTACK *temp = filestack;
  1046.  
  1047.   if (!filestack)
  1048.     abort ();            /* My fault.  I wonder what I did? */
  1049.  
  1050.   /* Make sure that commands with braces have been satisfied. */
  1051.   if (!executing_string)
  1052.     discard_braces ();
  1053.  
  1054.   /* Get the top of the stack into the globals. */
  1055.   input_filename = filestack->filename;
  1056.   input_text = filestack->text;
  1057.   size_of_input_text = filestack->size;
  1058.   input_text_offset = filestack->offset;
  1059.   line_number = filestack->line_number;
  1060.  
  1061.   /* Pop the stack. */
  1062.   filestack = filestack->next;
  1063.   free (temp);
  1064.   pop_node_filename ();
  1065. }
  1066.  
  1067. /* Flush all open files on the file stack. */
  1068. flush_file_stack ()
  1069. {
  1070.   while (filestack)
  1071.     {
  1072.       free (input_filename);
  1073.       free (input_text);
  1074.       popfile ();
  1075.     }
  1076. }
  1077.  
  1078. int node_filename_stack_index = 0;
  1079. int node_filename_stack_size = 0;
  1080. char **node_filename_stack = (char **)NULL;
  1081.  
  1082. push_node_filename ()
  1083. {
  1084.   if (node_filename_stack_index + 1 > node_filename_stack_size)
  1085.     {
  1086.       if (!node_filename_stack)
  1087.     node_filename_stack =
  1088.       (char **)xmalloc ((node_filename_stack_size += 10)
  1089.                 * sizeof (char *));
  1090.       else
  1091.     node_filename_stack =
  1092.       (char **)xrealloc (node_filename_stack,
  1093.                  (node_filename_stack_size + 10)
  1094.                  * sizeof (char *));
  1095.     }
  1096.  
  1097.   node_filename_stack[node_filename_stack_index] = node_filename;
  1098.   node_filename_stack_index++;
  1099. }
  1100.  
  1101. pop_node_filename ()
  1102. {
  1103.   node_filename = node_filename_stack[--node_filename_stack_index];
  1104. }
  1105.  
  1106. /* Return just the simple part of the filename; i.e. the
  1107.    filename without the path information, or extensions.
  1108.    This conses up a new string. */
  1109. char *
  1110. filename_part (filename)
  1111.      char *filename;
  1112. {
  1113.   register int i = strlen (filename) - 1;
  1114.  
  1115.   while (i && filename[i] != '/')
  1116.     i--;
  1117.   if (filename[i] == '/')
  1118.     i++;
  1119.  
  1120. #if defined (REMOVE_OUTPUT_EXTENSIONS)
  1121.   result = savestring (&filename[i]);
  1122.  
  1123.   /* See if there is an extension to remove.  If so, remove it. */
  1124.   if (rindex (result, '.'))
  1125.     *(rindex (result, '.')) = '\0';
  1126.   return (result);
  1127. #else
  1128.   return (savestring (&filename[i]));
  1129. #endif /* REMOVE_OUTPUT_EXTENSIONS */
  1130. }
  1131.  
  1132. /* Return the pathname part of filename.  This can be NULL. */
  1133. char *
  1134. pathname_part (filename)
  1135.      char *filename;
  1136. {
  1137.   char *expand_filename ();
  1138.   char *result = (char *) NULL;
  1139.   register int i;
  1140.  
  1141.   filename = expand_filename (filename, "");
  1142.  
  1143.   i = strlen (filename) - 1;
  1144.  
  1145.   while (i && filename[i] != '/')
  1146.     i--;
  1147.   if (filename[i] == '/')
  1148.     i++;
  1149.  
  1150.   if (i)
  1151.     {
  1152.       result = (char *)xmalloc (1 + i);
  1153.       strncpy (result, filename, i);
  1154.       result[i] = '\0';
  1155.     }
  1156.   free (filename);
  1157.   return (result);
  1158. }
  1159.  
  1160. /* Return the expansion of FILENAME. */
  1161. char *
  1162. expand_filename (filename, input_name)
  1163.      char *filename, *input_name;
  1164. {
  1165.   char *full_pathname ();
  1166.   filename = full_pathname (filename);
  1167.  
  1168.   if (filename[0] == '.')
  1169.     return (filename);
  1170.  
  1171.   if (filename[0] != '/' && input_name[0] == '/')
  1172.     {
  1173.       /* Make it so that relative names work. */
  1174.       char *result;
  1175.       int i = strlen (input_name) - 1;
  1176.  
  1177.       result = (char *)xmalloc (1 + strlen (input_name) + strlen (filename));
  1178.       strcpy (result, input_name);
  1179.  
  1180.       while (result[i] != '/' && i)
  1181.     i--;
  1182.  
  1183.       if (result[i] == '/')
  1184.     i++;
  1185.  
  1186.       strcpy (&result[i], filename);
  1187.       free (filename);
  1188.       return (result);
  1189.     }
  1190.   return (filename);
  1191. }
  1192.  
  1193. /* Return the full path to FILENAME. */
  1194. char *
  1195. full_pathname (filename)
  1196.      char *filename;
  1197. {
  1198.   int initial_character;
  1199.  
  1200.   if (filename && (initial_character = *filename))
  1201.     {
  1202.       if (initial_character == '/')
  1203.     return (savestring (filename));
  1204.       if (initial_character != '~')
  1205.     {
  1206.       return (savestring (filename));
  1207.     }
  1208.       else
  1209.     {
  1210.       if (filename[1] == '/')
  1211.         {
  1212.           /* Return the concatenation of HOME and the rest of the string. */
  1213.           char *temp_home;
  1214.           char *temp_name;
  1215.  
  1216.           temp_home = (char *) getenv ("HOME");
  1217.           temp_name = (char *)xmalloc (strlen (&filename[2])
  1218.                        + 1
  1219.                        + temp_home ? strlen (temp_home)
  1220.                        : 0);
  1221.           if (temp_home)
  1222.         strcpy (temp_name, temp_home);
  1223.  
  1224.           strcat (temp_name, &filename[2]);
  1225.           return (temp_name);
  1226.         }
  1227.       else
  1228.         {
  1229.           struct passwd *user_entry;
  1230.           int i, c;
  1231.           char *username = (char *)xmalloc (257);
  1232.           char *temp_name;
  1233.  
  1234.           for (i = 1; c = filename[i]; i++)
  1235.         {
  1236.           if (c == '/')
  1237.             break;
  1238.           else
  1239.             username[i - 1] = c;
  1240.         }
  1241.           if (c)
  1242.         username[i - 1] = '\0';
  1243.  
  1244.           user_entry = getpwnam (username);
  1245.  
  1246.           if (!user_entry)
  1247.         return (savestring (filename));
  1248.  
  1249.           temp_name = (char *)xmalloc (1 + strlen (user_entry->pw_dir)
  1250.                        + strlen (&filename[i]));
  1251.           strcpy (temp_name, user_entry->pw_dir);
  1252.           strcat (temp_name, &filename[i]);
  1253.           return (temp_name);
  1254.         }
  1255.     }
  1256.     }
  1257.   else
  1258.     {
  1259.       return (savestring (filename));
  1260.     }
  1261. }
  1262.  
  1263. /* **************************************************************** */
  1264. /*                                    */
  1265. /*            Error Handling                    */
  1266. /*                                    */
  1267. /* **************************************************************** */
  1268.  
  1269. /* Number of errors encountered. */
  1270. int errors_printed = 0;
  1271.  
  1272. /* Print the last error gotten from the file system. */
  1273. fs_error (filename)
  1274.      char *filename;
  1275. {
  1276.   remember_error ();
  1277.   perror (filename);
  1278.   return (0);
  1279. }
  1280.  
  1281. /* Print an error message, and return false. */
  1282. error (format, arg1, arg2, arg3, arg4, arg5)
  1283.      char *format;
  1284. {
  1285.   remember_error ();
  1286.   fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
  1287.   fprintf (stderr, "\n");
  1288.   return ((int) 0);
  1289. }
  1290.  
  1291. /* Just like error (), but print the line number as well. */
  1292. line_error (format, arg1, arg2, arg3, arg4, arg5)
  1293.      char *format;
  1294. {
  1295.   remember_error ();
  1296.   fprintf (stderr, "%s:%d: ", input_filename, line_number);
  1297.   fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
  1298.   fprintf (stderr, ".\n");
  1299.   return ((int) 0);
  1300. }
  1301.  
  1302. warning (format, arg1, arg2, arg3, arg4, arg5)
  1303.      char *format;
  1304. {
  1305.   if (print_warnings)
  1306.     {
  1307.       fprintf (stderr, "%s:%d: Warning: ", input_filename, line_number);
  1308.       fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
  1309.       fprintf (stderr, ".\n");
  1310.     }
  1311.   return ((int) 0);
  1312. }
  1313.  
  1314. /* Remember that an error has been printed.  If this is the first
  1315.    error printed, then tell them which program is printing them.
  1316.    If more than max_error_level have been printed, then exit the
  1317.    program. */
  1318. remember_error ()
  1319. {
  1320.   errors_printed++;
  1321.   if (max_error_level && (errors_printed > max_error_level))
  1322.     {
  1323.       fprintf (stderr, "Too many errors!  Gave up.\n");
  1324.       flush_file_stack ();
  1325.       cm_bye ();
  1326.       exit (1);
  1327.     }
  1328. }
  1329.  
  1330.  
  1331. /* **************************************************************** */
  1332. /*                                    */
  1333. /*            Hacking Tokens and Strings            */
  1334. /*                                    */
  1335. /* **************************************************************** */
  1336.  
  1337. /* Return the next token as a string pointer.  We cons the
  1338.    string. */
  1339. char *
  1340. read_token ()
  1341. {
  1342.   int i, character;
  1343.   char *result;
  1344.  
  1345.   /* Hack special case.  If the first character to be read is
  1346.      self-delimiting, then that is the command itself. */
  1347.  
  1348.   character = curchar ();
  1349.   if (self_delimiting (character))
  1350.     {
  1351.       input_text_offset++;
  1352.       result = savestring (" ");
  1353.       *result = character;
  1354.       return (result);
  1355.     }
  1356.  
  1357.   for (i = 0; ((input_text_offset != size_of_input_text)
  1358.            && (character = curchar ())
  1359.            && command_char (character));
  1360.        i++, input_text_offset++);
  1361.   result = (char *)xmalloc (i + 1);
  1362.   bcopy (&input_text[input_text_offset - i], result, i);
  1363.   result[i] = '\0';
  1364.   return (result);
  1365. }
  1366.  
  1367. /* Return non-zero if CHARACTER is self-delimiting. */
  1368. int
  1369. self_delimiting (character)
  1370.      int character;
  1371. {
  1372.   return (member (character, "{}:.@*'`,!?; \n"));
  1373. }
  1374.  
  1375. /* Clear whitespace from the front and end of string. */
  1376. canon_white (string)
  1377.      char *string;
  1378. {
  1379.   int len = strlen (string);
  1380.   int x;
  1381.  
  1382.   if (!len)
  1383.     return;
  1384.  
  1385.   for (x = 0; x < len; x++)
  1386.     {
  1387.       if (!whitespace (string[x]))
  1388.     {
  1389.       strcpy (string, string + x);
  1390.       break;
  1391.     }
  1392.     }
  1393.   len = strlen (string);
  1394.   if (len)
  1395.     len--;
  1396.   while (len > -1 && cr_or_whitespace (string[len]))
  1397.     len--;
  1398.   string[len + 1] = '\0';
  1399. }
  1400.  
  1401. /* Bash STRING, replacing all whitespace with just one space. */
  1402. fix_whitespace (string)
  1403.      char *string;
  1404. {
  1405.   char *temp = (char *)xmalloc (strlen (string) + 1);
  1406.   int string_index = 0;
  1407.   int temp_index = 0;
  1408.   int c;
  1409.  
  1410.   canon_white (string);
  1411.  
  1412.   while (string[string_index])
  1413.     {
  1414.       c = temp[temp_index++] = string[string_index++];
  1415.  
  1416.       if (c == ' ' || c == '\n' || c == '\t')
  1417.     {
  1418.       temp[temp_index - 1] = ' ';
  1419.       while ((c = string[string_index]) && (c == ' ' ||
  1420.                         c == '\t' ||
  1421.                         c == '\n'))
  1422.         string_index++;
  1423.     }
  1424.     }
  1425.   temp[temp_index] = '\0';
  1426.   strcpy (string, temp);
  1427.   free (temp);
  1428. }
  1429.  
  1430. /* Discard text until the desired string is found.  The string is
  1431.    included in the discarded text. */
  1432. discard_until (string)
  1433.      char *string;
  1434. {
  1435.   int temp = search_forward (string, input_text_offset);
  1436.  
  1437.   int tt = (temp < 0) ? size_of_input_text : temp + strlen (string);
  1438.   int from = input_text_offset;
  1439.  
  1440.   /* Find out what line we are on. */
  1441.   while (from != tt)
  1442.     if (input_text[from++] == '\n')
  1443.       line_number++;
  1444.  
  1445.   if (temp < 0)
  1446.     {
  1447.       input_text_offset = size_of_input_text - strlen (string);
  1448.  
  1449.       if (strcmp (string, "\n") != 0)
  1450.     {
  1451.       line_error ("Expected `%s'", string);
  1452.       return;
  1453.     }
  1454.     }
  1455.   else
  1456.     input_text_offset = temp;
  1457.  
  1458.   input_text_offset += strlen (string);
  1459. }
  1460.  
  1461. /* Read characters from the file until we are at MATCH.
  1462.    Place the characters read into STRING.
  1463.    On exit input_text_offset is after the match string.
  1464.    Return the offset where the string starts. */
  1465. int
  1466. get_until (match, string)
  1467.      char *match, **string;
  1468. {
  1469.   int len, current_point, x, new_point, tem;
  1470.  
  1471.   current_point = x = input_text_offset;
  1472.   new_point = search_forward (match, input_text_offset);
  1473.  
  1474.   if (new_point < 0)
  1475.     new_point = size_of_input_text;
  1476.   len = new_point - current_point;
  1477.  
  1478.   /* Keep track of which line number we are at. */
  1479.   tem = new_point + (strlen (match) - 1);
  1480.   while (x != tem)
  1481.     if (input_text[x++] == '\n')
  1482.       line_number++;
  1483.  
  1484.   *string = (char *)xmalloc (len + 1);
  1485.  
  1486.   bcopy (&input_text[current_point], *string, len);
  1487.   (*string)[len] = '\0';
  1488.  
  1489.   /* Now leave input_text_offset in a consistent state. */
  1490.   input_text_offset = tem;
  1491.  
  1492.   if (input_text_offset > size_of_input_text)
  1493.     input_text_offset = size_of_input_text;
  1494.  
  1495.   return (new_point);
  1496. }
  1497.  
  1498. /* Read characters from the file until we are at MATCH or end of line.
  1499.    Place the characters read into STRING.  */
  1500. get_until_in_line (match, string)
  1501.      char *match, **string;
  1502. {
  1503.   int real_bottom, temp;
  1504.  
  1505.   real_bottom = size_of_input_text;
  1506.   temp = search_forward ("\n", input_text_offset);
  1507.  
  1508.   if (temp < 0)
  1509.     temp = size_of_input_text;
  1510.  
  1511.   size_of_input_text = temp;
  1512.   get_until (match, string);
  1513.   size_of_input_text = real_bottom;
  1514. }
  1515.  
  1516. get_rest_of_line (string)
  1517.      char **string;
  1518. {
  1519.   get_until ("\n", string);
  1520.   canon_white (*string);
  1521.  
  1522.   if (curchar () == '\n')    /* as opposed to the end of the file... */
  1523.     {
  1524.       line_number++;
  1525.       input_text_offset++;
  1526.     }
  1527. }
  1528.  
  1529. /* Backup the input pointer to the previous character, keeping track
  1530.    of the current line number. */
  1531. backup_input_pointer ()
  1532. {
  1533.   if (input_text_offset)
  1534.     {
  1535.       input_text_offset--;
  1536.       if (curchar () == '\n')
  1537.     line_number--;
  1538.     }
  1539. }
  1540.  
  1541. /* Read characters from the file until we are at MATCH or closing brace.
  1542.    Place the characters read into STRING.  */
  1543. get_until_in_braces (match, string)
  1544.      char *match, **string;
  1545. {
  1546.   int i, brace = 0;
  1547.   int match_len = strlen (match);
  1548.   char *temp;
  1549.  
  1550.   for (i = input_text_offset; i < size_of_input_text; i++)
  1551.     {
  1552.       if (input_text[i] == '{')
  1553.     brace++;
  1554.       else if (input_text[i] == '}')
  1555.     brace--;
  1556.       else if (input_text[i] == '\n')
  1557.     line_number++;
  1558.  
  1559.       if (brace < 0 ||
  1560.       (brace == 0 && strncmp (input_text + i, match, match_len) == 0))
  1561.     break;
  1562.     }
  1563.  
  1564.   match_len = i - input_text_offset;
  1565.   temp = (char *)xmalloc (2 + match_len);
  1566.   strncpy (temp, input_text + input_text_offset, match_len);
  1567.   temp[match_len] = '\0';
  1568.   input_text_offset = i;
  1569.   *string = temp;
  1570. }
  1571.  
  1572. /* **************************************************************** */
  1573. /*                                    */
  1574. /*            Converting the File                 */
  1575. /*                                    */
  1576. /* **************************************************************** */
  1577.  
  1578. /* Convert the file named by NAME.  The output is saved on the file
  1579.    named as the argument to the @setfilename command. */
  1580. static char *suffixes[] = {
  1581.   "",
  1582.   ".tex",
  1583.   ".texinfo",
  1584.   ".texi",
  1585.   ".txinfo",
  1586.   (char *)NULL
  1587. };
  1588.  
  1589. convert (name)
  1590.      char *name;
  1591. {
  1592.       char *real_output_filename, *expand_filename (), *filename_part ();
  1593.       char *filename = (char *)xmalloc (strlen (name) + 50);
  1594.       register int i;
  1595.  
  1596.       init_tag_table ();
  1597.       init_indices ();
  1598.       init_internals ();
  1599.       init_paragraph ();
  1600.  
  1601.       /* Try to load the file specified by NAME.  If the file isn't found, and
  1602.          there is no suffix in NAME, then try NAME.texinfo, and NAME.texi, and
  1603.          NAME.tex. */
  1604.       for (i = 0; suffixes[i]; i++)
  1605.         {
  1606.           strcpy (filename, name);
  1607.           strcat (filename, suffixes[i]);
  1608.  
  1609.           if (find_and_load (filename))
  1610.         break;
  1611.  
  1612.           if (!suffixes[i][0] && rindex (filename, '.'))
  1613.         {
  1614.           fs_error (filename);
  1615.           free (filename);
  1616.           return;
  1617.         }
  1618.         }
  1619.  
  1620.       if (!suffixes[i])
  1621.         {
  1622.           fs_error (name);
  1623.           free (filename);
  1624.           return;
  1625.         }
  1626.  
  1627.       input_filename = filename;
  1628.  
  1629.       /* Search this file looking for the special string which starts conversion.
  1630.          Once found, we may truly begin. */
  1631.  
  1632.       input_text_offset = search_forward ("@setfilename", 0);
  1633.  
  1634.       if (input_text_offset < 0)
  1635.         {
  1636.           if (!command_output_filename)
  1637.         {
  1638.           error ("No `@setfilename' found in `%s'", name);
  1639.           goto finished;
  1640.         }
  1641.         }
  1642.       else
  1643.         input_text_offset += strlen ("@setfilename");
  1644.  
  1645.       real_output_filename = (char *)NULL;
  1646.  
  1647.       if (!command_output_filename)
  1648.         get_until ("\n", &output_filename);
  1649.       else
  1650.         {
  1651.           discard_until ("\n");
  1652.           real_output_filename = output_filename = command_output_filename;
  1653.           command_output_filename = (char *)NULL;
  1654.         }
  1655.  
  1656.       canon_white (output_filename);
  1657.       printf ("Making info file `%s' from `%s'.\n", output_filename, name);
  1658.  
  1659.       if (verbose_mode)
  1660.         fprintf (stderr, "  The input file contains %d characters.\n",
  1661.              size_of_input_text);
  1662.  
  1663.       if (real_output_filename &&
  1664.           strcmp (real_output_filename, "-") == 0)
  1665.         {
  1666.           output_stream = stdout;
  1667.         }
  1668.       else
  1669.         {
  1670.           if (!real_output_filename)
  1671.         real_output_filename = expand_filename (output_filename, name);
  1672.           output_stream = fopen (real_output_filename, "w");
  1673.           current_file_size = 0;
  1674.         }
  1675.  
  1676.       if (output_stream == NULL)
  1677.         {
  1678.           fs_error (real_output_filename);
  1679.           goto finished;
  1680.         }
  1681.  
  1682.       /* Make the displayable filename from output_filename.  Only the base
  1683.          portion of the filename need be displayed. */
  1684.       pretty_output_filename = filename_part (output_filename);
  1685.  
  1686.       /* For this file only, count the number of newlines from the top of
  1687.          the file to here.  This way, we keep track of line numbers for
  1688.          error reporting.  Line_number starts at 1, since the user isn't
  1689.          zero-based. */
  1690.       {
  1691.         int temp = 0;
  1692.         line_number = 1;
  1693.         while (temp != input_text_offset)
  1694.           if (input_text[temp++] == '\n')
  1695.         line_number++;
  1696.       }
  1697.  
  1698.       add_word_args ("This is Info file %s, produced by Makeinfo-%d.%d from ",
  1699.              output_filename, major_version, minor_version);
  1700.       add_word_args ("the input file %s.\n", input_filename);
  1701.       close_paragraph ();
  1702.  
  1703.       reader_loop ();
  1704.  
  1705.     finished:
  1706.       close_paragraph ();
  1707.       flush_file_stack ();
  1708.       if (output_stream != NULL)
  1709.        {
  1710.           output_pending_notes ();
  1711.           free_pending_notes ();
  1712.           if (tag_table != NULL)
  1713.         {
  1714.               tag_table = (TAG_ENTRY *) reverse_list (tag_table);
  1715.               write_tag_table ();
  1716.         }
  1717.  
  1718.           if (output_stream != stdout)
  1719.             fclose (output_stream);
  1720.  
  1721.           /* If validating, then validate the entire file right now. */
  1722.           if (validating)
  1723.             validate_file (real_output_filename, tag_table);
  1724.  
  1725.           /* This used to test  && !errors_printed.
  1726.          But some files might have legit warnings.  So split anyway.  */
  1727.           if (splitting)
  1728.             split_file (real_output_filename, 0);
  1729.        }
  1730. }
  1731.  
  1732. free_and_clear (pointer)
  1733.      char **pointer;
  1734. {
  1735.   if ((*pointer) != (char *) NULL)
  1736.     {
  1737.       free (*pointer);
  1738.       *pointer = (char *) NULL;
  1739.     }
  1740. }
  1741.  
  1742.  /* Initialize some state. */
  1743. init_internals ()
  1744. {
  1745.   free_and_clear (¤t_node);
  1746.   free_and_clear (&output_filename);
  1747.   free_and_clear (&command);
  1748.   free_and_clear (&input_filename);
  1749.   free_node_references ();
  1750.   init_insertion_stack ();
  1751.   init_brace_stack ();
  1752.   command_index = 0;
  1753.   in_menu = 0;
  1754. }
  1755.  
  1756. init_paragraph ()
  1757. {
  1758.   free_and_clear (&output_paragraph);
  1759.   output_paragraph = (unsigned char *)xmalloc (paragraph_buffer_len);
  1760.   output_position = 0;
  1761.   output_paragraph[0] = '\0';
  1762.   output_paragraph_offset = 0;
  1763.   output_column = 0;
  1764.   paragraph_is_open = 0;
  1765.   current_indent = 0;
  1766. }
  1767.  
  1768. /* Okay, we are ready to start the conversion.  Call the reader on
  1769.    some text, and fill the text as it is output.  Handle commands by
  1770.    remembering things like open braces and the current file position on a
  1771.    stack, and when the corresponding close brace is found, you can call
  1772.    the function with the proper arguments. */
  1773. reader_loop ()
  1774. {
  1775.   int character;
  1776.   int done = 0;
  1777.   int dash_count = 0;
  1778.  
  1779.   while (!done)
  1780.     {
  1781.       if (input_text_offset >= size_of_input_text)
  1782.     {
  1783.       if (filestack)
  1784.         {
  1785.           free (input_filename);
  1786.           free (input_text);
  1787.           popfile ();
  1788.         }
  1789.       else
  1790.         break;
  1791.     }
  1792.  
  1793.       character = curchar ();
  1794.  
  1795.       if (!in_fixed_width_font &&
  1796.       (character == '\'' || character == '`') &&
  1797.       input_text[input_text_offset + 1] == character)
  1798.     {
  1799.       input_text_offset++;
  1800.       character = '"';
  1801.     }
  1802.  
  1803.       if (character == '-')
  1804.     {
  1805.       dash_count++;
  1806.       if (dash_count == 3 && !in_fixed_width_font)
  1807.         {
  1808.           input_text_offset++;
  1809.           continue;
  1810.         }
  1811.     }
  1812.       else
  1813.     {
  1814.       dash_count = 0;
  1815.     }
  1816.  
  1817.       /* If this is a whitespace character, then check to see if the line
  1818.      is blank.  If so, advance to the carriage return. */
  1819.       if (whitespace (character))
  1820.     {
  1821.       register int i = input_text_offset + 1;
  1822.  
  1823.       while (i < size_of_input_text && whitespace (input_text[i]))
  1824.         i++;
  1825.  
  1826.       if (i == size_of_input_text || input_text[i] == '\n')
  1827.         {
  1828.           if (i == size_of_input_text)
  1829.         i--;
  1830.  
  1831.           input_text_offset = i;
  1832.           character = curchar ();
  1833.         }
  1834.     }
  1835.  
  1836.       if (character == '\n')
  1837.     {
  1838.       line_number++;
  1839.       if (in_menu && input_text_offset + 1 < size_of_input_text)
  1840.         {
  1841.           char *glean_node_from_menu (), *tem;
  1842.  
  1843.           /* Note that the value of TEM is discarded, since it is
  1844.          gauranteed to be NULL when glean_node_from_menu () is
  1845.          called with a non-zero argument. */
  1846.           tem = glean_node_from_menu (1);
  1847.         }
  1848.     }
  1849.  
  1850.       switch (character)
  1851.     {
  1852.     case COMMAND_PREFIX:
  1853.       read_command ();
  1854.       if (strcmp (command, "bye") == 0)
  1855.         {
  1856.           done = 1;
  1857.           continue;
  1858.         }
  1859.       break;
  1860.  
  1861.     case '{':
  1862.  
  1863.       /* Special case.  I'm not supposed to see this character by itself.
  1864.          If I do, it means there is a syntax error in the input text.
  1865.          Report the error here, but remember this brace on the stack so
  1866.          you can ignore its partner. */
  1867.  
  1868.       line_error ("Misplaced `{'");
  1869.       remember_brace (misplaced_brace);
  1870.  
  1871.       /* Don't advance input_text_offset since this happens in
  1872.          remember_brace ().
  1873.          input_text_offset++;
  1874.            */
  1875.       break;
  1876.  
  1877.     case '}':
  1878.       pop_and_call_brace ();
  1879.       input_text_offset++;
  1880.       break;
  1881.  
  1882.     default:
  1883.       add_char (character);
  1884.       input_text_offset++;
  1885.     }
  1886.     }
  1887. }
  1888.  
  1889. /* Find the command corresponding to STRING.  If the command
  1890.    is found, return a pointer to the data structure.  Otherwise
  1891.    return (-1). */
  1892. COMMAND *
  1893. get_command_entry (string)
  1894.      char *string;
  1895. {
  1896.   register int i;
  1897.  
  1898.   for (i = 0; CommandTable[i].name; i++)
  1899.     if (strcmp (CommandTable[i].name, string) == 0)
  1900.       return (&CommandTable[i]);
  1901.  
  1902.   /* This command is not in our predefined command table.  Perhaps
  1903.      it is a user defined command. */
  1904.   for (i = 0; i < user_command_array_len; i++)
  1905.     if (user_command_array[i] &&
  1906.     (strcmp (user_command_array[i]->name, string) == 0))
  1907.       return (user_command_array[i]);
  1908.  
  1909.   /* Nope, we never heard of this command. */
  1910.   return ((COMMAND *) -1);
  1911. }
  1912.  
  1913. /* input_text_offset is right at the command prefix character.
  1914.    Read the next token to determine what to do. */
  1915. read_command ()
  1916. {
  1917.   COMMAND *entry;
  1918.   input_text_offset++;
  1919.   free_and_clear (&command);
  1920.   command = read_token ();
  1921.  
  1922. #if defined (HAVE_MACROS)
  1923.   /* Check to see if this command is a macro.  If so, execute it here. */
  1924.   {
  1925.     MACRO_DEF *def;
  1926.  
  1927.     def = find_macro (command);
  1928.  
  1929.     if (def)
  1930.       {
  1931.     execute_macro (def);
  1932.     return;
  1933.       }
  1934.   }
  1935. #endif /* HAVE_MACROS */
  1936.  
  1937.   entry = get_command_entry (command);
  1938.  
  1939.   if ((int) entry == -1)
  1940.     {
  1941.       line_error ("Unknown info command `%s'", command);
  1942.       return;
  1943.     }
  1944.  
  1945.   if (entry->argument_in_braces)
  1946.     remember_brace (entry->proc);
  1947.  
  1948.   (*(entry->proc)) (START);
  1949. }
  1950.  
  1951. /* Return the string which invokes PROC; a pointer to a function. */
  1952. char *
  1953. find_proc_name (proc)
  1954.      FUNCTION *proc;
  1955. {
  1956.   register int i;
  1957.  
  1958.   for (i = 0; CommandTable[i].name; i++)
  1959.     if (proc == CommandTable[i].proc)
  1960.       return (CommandTable[i].name);
  1961.   return ("NO_NAME!");
  1962. }
  1963.  
  1964. init_brace_stack ()
  1965. {
  1966.   brace_stack = (BRACE_ELEMENT *) NULL;
  1967. }
  1968.  
  1969. remember_brace (proc)
  1970.      FUNCTION *proc;
  1971. {
  1972.   if (curchar () != '{')
  1973.     line_error ("@%s expected `{..}'", command);
  1974.   else
  1975.     input_text_offset++;
  1976.   remember_brace_1 (proc, output_paragraph_offset);
  1977. }
  1978.  
  1979. /* Remember the current output position here.  Save PROC
  1980.    along with it so you can call it later. */
  1981. remember_brace_1 (proc, position)
  1982.      FUNCTION *proc;
  1983.      int position;
  1984. {
  1985.   BRACE_ELEMENT *new = (BRACE_ELEMENT *) xmalloc (sizeof (BRACE_ELEMENT));
  1986.   new->next = brace_stack;
  1987.   new->proc = proc;
  1988.   new->pos = position;
  1989.   new->line = line_number;
  1990.   brace_stack = new;
  1991. }
  1992.  
  1993. /* Pop the top of the brace stack, and call the associated function
  1994.    with the args END and POS. */
  1995. pop_and_call_brace ()
  1996. {
  1997.   BRACE_ELEMENT *temp;
  1998.   FUNCTION *proc;
  1999.   int pos;
  2000.  
  2001.   if (brace_stack == (BRACE_ELEMENT *) NULL)
  2002.     return (line_error ("Unmatched close brace"));
  2003.  
  2004.   pos = brace_stack->pos;
  2005.   proc = brace_stack->proc;
  2006.   temp = brace_stack->next;
  2007.   free (brace_stack);
  2008.   brace_stack = temp;
  2009.  
  2010.   return ((*proc) (END, pos, output_paragraph_offset));
  2011. }
  2012.  
  2013. /* You call discard_braces () when you shouldn't have any braces on the stack.
  2014.    I used to think that this happens for commands that don't take arguments
  2015.    in braces, but that was wrong because of things like @code{foo @@}.  So now
  2016.    I only detect it at the beginning of nodes. */
  2017. discard_braces ()
  2018. {
  2019.   int temp_line_number = line_number;
  2020.   char *proc_name;
  2021.  
  2022.   if (!brace_stack)
  2023.     return;
  2024.  
  2025.   while (brace_stack)
  2026.     {
  2027.       line_number = brace_stack->line;
  2028.       proc_name = find_proc_name (brace_stack->proc);
  2029.       line_error ("@%s missing close brace", proc_name);
  2030.       line_number = temp_line_number;
  2031.       pop_and_call_brace ();
  2032.     }
  2033. }
  2034.  
  2035. get_char_len (character)
  2036.      int character;
  2037. {
  2038.   /* Return the printed length of the character. */
  2039.   int len;
  2040.  
  2041.   switch (character)
  2042.     {
  2043.     case '\t':
  2044.       len = (output_column + 8) & 0xf7;
  2045.       if (len > fill_column)
  2046.     len = fill_column - output_column;
  2047.       else
  2048.     len = len - output_column;
  2049.       break;
  2050.  
  2051.     case '\n':
  2052.       len = fill_column - output_column;
  2053.       break;
  2054.  
  2055.     default:
  2056.       if (character < ' ')
  2057.     len = 2;
  2058.       else
  2059.     len = 1;
  2060.     }
  2061.   return (len);
  2062. }
  2063.  
  2064. add_word_args (format, arg1, arg2, arg3, arg4, arg5)
  2065.      char *format;
  2066. {
  2067.   char buffer[1000];
  2068.   sprintf (buffer, format, arg1, arg2, arg3, arg4, arg5);
  2069.   add_word (buffer);
  2070. }
  2071.  
  2072. /* Add STRING to output_paragraph. */
  2073. add_word (string)
  2074.      char *string;
  2075. {
  2076.   while (*string)
  2077.     add_char (*string++);
  2078. }
  2079.  
  2080. int last_char_was_newline = 1;
  2081. int last_inserted_character = 0;
  2082.  
  2083. /* Non-zero means that a newline character has already been
  2084.    inserted, so close_paragraph () should insert one less. */
  2085. int line_already_broken = 0;
  2086.  
  2087. /* Add the character to the current paragraph.  If filling_enabled is
  2088.    non-zero, then do filling as well. */
  2089. add_char (character)
  2090.      int character;
  2091. {
  2092.   /* If we are avoiding outputting headers, and we are currently
  2093.      in a menu, then simply return. */
  2094.   if (no_headers && in_menu)
  2095.     return;
  2096.  
  2097.   /* If we are adding a character now, then we don't have to
  2098.      ignore close_paragraph () calls any more. */
  2099.   if (must_start_paragraph && character != '\n')
  2100.     {
  2101.       must_start_paragraph = 0;
  2102.       line_already_broken = 0;    /* The line is no longer broken. */
  2103.       if (current_indent > output_column)
  2104.     {
  2105.       indent (current_indent - output_column);
  2106.       output_column = current_indent;
  2107.     }
  2108.     }
  2109.  
  2110.   if (non_splitting_words && member (character, " \t\n"))
  2111.     character = ' ' | 0x80;
  2112.  
  2113.   switch (character)
  2114.     {
  2115.     case '\n':
  2116.       if (!filling_enabled)
  2117.     {
  2118.       insert ('\n');
  2119.  
  2120.       if (force_flush_right)
  2121.         {
  2122.           close_paragraph ();
  2123.           /* Hack to force single blank lines out in this mode. */
  2124.           flush_output ();
  2125.         }
  2126.  
  2127.       output_column = 0;
  2128.  
  2129.       if (!no_indent && paragraph_is_open)
  2130.         indent (output_column = current_indent);
  2131.       break;
  2132.     }
  2133.       else
  2134.     {
  2135.       if (sentence_ender (last_inserted_character))
  2136.         {
  2137.           insert (' ');
  2138.           output_column++;
  2139.           last_inserted_character = character;
  2140.         }
  2141.     }
  2142.  
  2143.       if (last_char_was_newline)
  2144.     {
  2145.       close_paragraph ();
  2146.       pending_indent = 0;
  2147.     }
  2148.       else
  2149.     {
  2150.       last_char_was_newline = 1;
  2151.       insert (' ');
  2152.       output_column++;
  2153.     }
  2154.       break;
  2155.  
  2156.     default:
  2157.       {
  2158.     int len = get_char_len (character);
  2159.  
  2160.     if ((character == ' ') && (last_char_was_newline))
  2161.       {
  2162.         if (!paragraph_is_open)
  2163.           {
  2164.         pending_indent++;
  2165.         return;
  2166.           }
  2167.       }
  2168.  
  2169.     if (!paragraph_is_open)
  2170.       {
  2171.         start_paragraph ();
  2172.  
  2173.         /* If the paragraph is supposed to be indented a certain way,
  2174.            then discard all of the pending whitespace.  Otherwise, we
  2175.            let the whitespace stay. */
  2176.         if (!paragraph_start_indent)
  2177.           indent (pending_indent);
  2178.         pending_indent = 0;
  2179.       }
  2180.  
  2181.     if ((output_column += len) >= fill_column)
  2182.       {
  2183.         if (filling_enabled)
  2184.           {
  2185.         int temp = output_paragraph_offset;
  2186.         while (--temp > 0 && output_paragraph[temp] != '\n')
  2187.           {
  2188.             /* If we have found a space, we have the place to break
  2189.                the line. */
  2190.             if (output_paragraph[temp] == ' ')
  2191.               {
  2192.             output_paragraph[temp++] = '\n';
  2193.  
  2194.             /* We have correctly broken the line where we want
  2195.                to.  What we don't want is spaces following where
  2196.                we have decided to break the line.  We get rid of
  2197.                them. */
  2198.             {
  2199.               int t1 = temp;
  2200.               while (t1 < output_paragraph_offset
  2201.                  && whitespace (output_paragraph[t1]))
  2202.                 t1++;
  2203.  
  2204.               if (t1 != temp)
  2205.                 {
  2206.                   strncpy ((char *) &output_paragraph[temp],
  2207.                        (char *) &output_paragraph[t1],
  2208.                        (output_paragraph_offset - t1));
  2209.                   output_paragraph_offset -= (t1 - temp);
  2210.                 }
  2211.             }
  2212.  
  2213.             /* Filled, but now indent if that is right. */
  2214.             if (indented_fill && current_indent)
  2215.               {
  2216.                 int buffer_len = ((output_paragraph_offset - temp)
  2217.                           + current_indent);
  2218.                 char *temp_buffer = (char *)xmalloc (buffer_len);
  2219.                 int indentation = 0;
  2220.  
  2221.                 /* We have to shift any markers that are in
  2222.                    front of the wrap point. */
  2223.                 {
  2224.                   register BRACE_ELEMENT *stack = brace_stack;
  2225.  
  2226.                   while (stack)
  2227.                 {
  2228.                   if (stack->pos > temp)
  2229.                     stack->pos += current_indent;
  2230.                   stack = stack->next;
  2231.                 }
  2232.                 }
  2233.  
  2234.                 while (current_indent > 0 &&
  2235.                    indentation != current_indent)
  2236.                   temp_buffer[indentation++] = ' ';
  2237.  
  2238.                 strncpy ((char *) &temp_buffer[current_indent],
  2239.                      (char *) &output_paragraph[temp],
  2240.                      buffer_len - current_indent);
  2241.  
  2242.                 if (output_paragraph_offset + buffer_len
  2243.                 >= paragraph_buffer_len)
  2244.                   {
  2245.                 unsigned char *tt = xrealloc
  2246.                   (output_paragraph,
  2247.                    (paragraph_buffer_len += buffer_len));
  2248.                 output_paragraph = tt;
  2249.                   }
  2250.                 strncpy ((char *) &output_paragraph[temp],
  2251.                      temp_buffer, buffer_len);
  2252.                 output_paragraph_offset += current_indent;
  2253.                 free (temp_buffer);
  2254.               }
  2255.             output_column = 0;
  2256.             while (temp < output_paragraph_offset)
  2257.               output_column +=
  2258.                 get_char_len (output_paragraph[temp++]);
  2259.             output_column += len;
  2260.             break;
  2261.               }
  2262.           }
  2263.           }
  2264.       }
  2265.     insert (character);
  2266.     line_already_broken = 0;
  2267.     last_char_was_newline = 0;
  2268.     last_inserted_character = character;
  2269.       }
  2270.     }
  2271. }
  2272.  
  2273. /* Insert CHARACTER into OUTPUT_PARAGRAPH. */
  2274. insert (character)
  2275.      int character;
  2276. {
  2277.   output_paragraph[output_paragraph_offset++] = character;
  2278.   if (output_paragraph_offset == paragraph_buffer_len)
  2279.     {
  2280.       output_paragraph =
  2281.     xrealloc (output_paragraph, (paragraph_buffer_len += 100));
  2282.     }
  2283. }
  2284.  
  2285. /* Remove upto COUNT characters of whitespace from the
  2286.    the current output line.  If COUNT is less than zero,
  2287.    then remove until none left. */
  2288. kill_self_indent (count)
  2289.      int count;
  2290. {
  2291.   /* Handle infinite case first. */
  2292.   if (count < 0)
  2293.     {
  2294.       output_column = 0;
  2295.       while (output_paragraph_offset)
  2296.     {
  2297.       if (whitespace (output_paragraph[output_paragraph_offset - 1]))
  2298.         output_paragraph_offset--;
  2299.       else
  2300.         break;
  2301.     }
  2302.     }
  2303.   else
  2304.     {
  2305.       while (output_paragraph_offset && count--)
  2306.     if (whitespace (output_paragraph[output_paragraph_offset - 1]))
  2307.       output_paragraph_offset--;
  2308.     else
  2309.       break;
  2310.     }
  2311. }
  2312.  
  2313. flush_output ()
  2314. {
  2315.   register int i;
  2316.  
  2317.   if (!output_paragraph_offset)
  2318.     return;
  2319.  
  2320.   for (i = 0; i < output_paragraph_offset; i++)
  2321.     {
  2322.       if (output_paragraph[i] == (unsigned char)(' ' | 0x80) ||
  2323.       output_paragraph[i] == (unsigned char)('\t' | 0x80) ||
  2324.       output_paragraph[i] == (unsigned char)('\n' | 0x80) ||
  2325.       sentence_ender (UNMETA (output_paragraph[i])))
  2326.     output_paragraph[i] &= 0x7f;
  2327.     }
  2328.  
  2329.   current_file_size += fwrite (output_paragraph, 1, output_paragraph_offset, output_stream);
  2330.   output_position += output_paragraph_offset;
  2331.   output_paragraph_offset = 0;
  2332. }
  2333.  
  2334. /* How to close a paragraph controlling the number of lines between
  2335.    this one and the last one. */
  2336.  
  2337. /* Paragraph spacing is controlled by this variable.  It is the number of
  2338.    blank lines that you wish to appear between paragraphs.  A value of
  2339.    1 creates a single blank line between paragraphs. */
  2340. int paragraph_spacing = DEFAULT_PARAGRAPH_SPACING;
  2341.  
  2342. /* Close the current paragraph, leaving no blank lines between them. */
  2343. close_single_paragraph ()
  2344. {
  2345.   close_paragraph_with_lines (0);
  2346. }
  2347.  
  2348. /* Close a paragraph after an insertion has ended. */
  2349. int insertion_paragraph_closed = 0;
  2350.  
  2351. close_insertion_paragraph ()
  2352. {
  2353.   if (!insertion_paragraph_closed)
  2354.     {
  2355.       /* Close the current paragraph, breaking the line. */
  2356.       close_single_paragraph ();
  2357.  
  2358.       /* Start a new paragraph here, inserting whatever indention is correct
  2359.      for the now current insertion level (one above the one that we are
  2360.      ending). */
  2361.       start_paragraph ();
  2362.  
  2363.       /* Tell close_paragraph () that the previous line has already been
  2364.      broken, so it should insert one less newline. */
  2365.       line_already_broken = 1;
  2366.  
  2367.       /* Let functions such as add_char () know that we have already found a
  2368.      newline. */
  2369.       ignore_blank_line ();
  2370.     }
  2371.  
  2372.   insertion_paragraph_closed = 1;
  2373. }
  2374.  
  2375. close_paragraph_with_lines (lines)
  2376.      int lines;
  2377. {
  2378.   int old_spacing = paragraph_spacing;
  2379.   paragraph_spacing = lines;
  2380.   close_paragraph ();
  2381.   paragraph_spacing = old_spacing;
  2382. }
  2383.  
  2384. /* Close the currently open paragraph. */
  2385. close_paragraph ()
  2386. {
  2387.   register int i;
  2388.  
  2389.   /* The insertion paragraph is no longer closed. */
  2390.   insertion_paragraph_closed = 0;
  2391.  
  2392.   if (paragraph_is_open && !must_start_paragraph)
  2393.     {
  2394.       register int tindex, c;
  2395.  
  2396.       tindex = output_paragraph_offset;
  2397.  
  2398.       /* Back up to last non-newline/space character, forcing all such
  2399.      subsequent characters to be newlines.  This isn't strictly
  2400.      necessary, but a couple of functions use the presence of a newline
  2401.      to make decisions. */
  2402.       for (tindex = output_paragraph_offset - 1; tindex >= 0; --tindex)
  2403.     {
  2404.       c = output_paragraph[tindex];
  2405.  
  2406.       if (c == ' '|| c == '\n')
  2407.         output_paragraph[tindex] = '\n';
  2408.       else
  2409.         break;
  2410.     }
  2411.  
  2412.       /* All trailing whitespace is ignored. */
  2413.       output_paragraph_offset = ++tindex;
  2414.  
  2415.       /* Break the line if that is appropriate. */
  2416.       if (paragraph_spacing >= 0)
  2417.     insert ('\n');
  2418.  
  2419.       /* Add as many blank lines as is specified in PARAGRAPH_SPACING. */
  2420.       if (!force_flush_right)
  2421.     {
  2422.       for (i = 0; i < (paragraph_spacing - line_already_broken); i++)
  2423.         insert ('\n');
  2424.     }
  2425.  
  2426.       /* If we are doing flush right indentation, then do it now
  2427.      on the paragraph (really a single line). */
  2428.       if (force_flush_right)
  2429.     do_flush_right_indentation ();
  2430.  
  2431.       flush_output ();
  2432.       paragraph_is_open = 0;
  2433.       no_indent = 0;
  2434.       output_column = 0;
  2435.     }
  2436.   ignore_blank_line ();
  2437. }
  2438.  
  2439. /* Make the last line just read look as if it were only a newline. */
  2440. ignore_blank_line ()
  2441. {
  2442.   last_inserted_character = '\n';
  2443.   last_char_was_newline = 1;
  2444. }
  2445.  
  2446. /* Align the end of the text in output_paragraph with fill_column. */
  2447. do_flush_right_indentation ()
  2448. {
  2449.   char *temp;
  2450.   int temp_len;
  2451.  
  2452.   kill_self_indent (-1);
  2453.  
  2454.   if (output_paragraph[0] != '\n')
  2455.     {
  2456.       output_paragraph[output_paragraph_offset] = '\0';
  2457.  
  2458.       if (output_paragraph_offset < fill_column)
  2459.     {
  2460.       register int i;
  2461.  
  2462.       if (fill_column >= paragraph_buffer_len)
  2463.         output_paragraph =
  2464.           xrealloc (output_paragraph,
  2465.             (paragraph_buffer_len += fill_column));
  2466.  
  2467.       temp_len = strlen ((char *)output_paragraph);
  2468.       temp = (char *)xmalloc (temp_len + 1);
  2469.       bcopy ((char *)output_paragraph, temp, temp_len);
  2470.  
  2471.       for (i = 0; i < fill_column - output_paragraph_offset; i++)
  2472.         output_paragraph[i] = ' ';
  2473.  
  2474.       bcopy (temp, (char *)output_paragraph + i, temp_len);
  2475.       free (temp);
  2476.       output_paragraph_offset = fill_column;
  2477.     }
  2478.     }
  2479. }
  2480.  
  2481. /* Begin a new paragraph. */
  2482. start_paragraph ()
  2483. {
  2484.   /* First close existing one. */
  2485.   if (paragraph_is_open)
  2486.     close_paragraph ();
  2487.  
  2488.   /* In either case, the insertion paragraph is no longer closed. */
  2489.   insertion_paragraph_closed = 0;
  2490.  
  2491.   /* However, the paragraph is open! */
  2492.   paragraph_is_open = 1;
  2493.  
  2494.   /* If we MUST_START_PARAGRAPH, that simply means that start_paragraph ()
  2495.      had to be called before we would allow any other paragraph operations
  2496.      to have an effect. */
  2497.   if (!must_start_paragraph)
  2498.     {
  2499.       int amount_to_indent = 0;
  2500.  
  2501.       /* If doing indentation, then insert the appropriate amount. */
  2502.       if (!no_indent)
  2503.     {
  2504.       if (inhibit_paragraph_indentation)
  2505.         {
  2506.           amount_to_indent = current_indent;
  2507.           if (inhibit_paragraph_indentation < 0)
  2508.         inhibit_paragraph_indentation++;
  2509.         }
  2510.       else if (paragraph_start_indent < 0)
  2511.         amount_to_indent = current_indent;
  2512.       else
  2513.         amount_to_indent = current_indent + paragraph_start_indent;
  2514.  
  2515.       if (amount_to_indent >= output_column)
  2516.         {
  2517.           amount_to_indent -= output_column;
  2518.           indent (amount_to_indent);
  2519.           output_column += amount_to_indent;
  2520.         }
  2521.     }
  2522.     }
  2523.   else
  2524.     must_start_paragraph = 0;
  2525. }
  2526.  
  2527. /* Insert the indentation specified by AMOUNT. */
  2528. indent (amount)
  2529.      int amount;
  2530. {
  2531.   while (--amount >= 0)
  2532.     insert (' ');
  2533. }
  2534.  
  2535. /* Search forward for STRING in input_text.
  2536.    FROM says where where to start. */
  2537. search_forward (string, from)
  2538.      char *string;
  2539.      int from;
  2540. {
  2541.   int len = strlen (string);
  2542.  
  2543.   while (from < size_of_input_text)
  2544.     {
  2545.       if (strnicmp (input_text + from, string, len) == 0)
  2546.     return (from);
  2547.       from++;
  2548.     }
  2549.   return (-1);
  2550. }
  2551.  
  2552. /* Whoops, Unix doesn't have stricmp, or strnicmp. */
  2553.  
  2554. enum insertion_type { menu, quotation, lisp, smalllisp, example,
  2555.   smallexample, display, itemize, format, enumerate, cartouche, table,
  2556.   ftable, vtable, group, ifinfo, flushleft, flushright, ifset, ifclear, deffn,
  2557.   defun, defmac, defspec, defvr, defvar, defopt, deftypefn,
  2558.   deftypefun, deftypevr, deftypevar, defcv, defivar, defop, defmethod,
  2559.   deftypemethod, deftp, bad_type };
  2560.  
  2561. char *insertion_type_names[] = { "menu", "quotation", "lisp",
  2562.   "smalllisp", "example", "smallexample", "display", "itemize",
  2563.   "format", "enumerate", "cartouche", "table", "ftable", "vtable", "group",
  2564.   "ifinfo", "flushleft", "flushright", "ifset", "ifclear", "deffn",
  2565.   "defun", "defmac", "defspec", "defvr", "defvar", "defopt",
  2566.   "deftypefn", "deftypefun", "deftypevr", "deftypevar", "defcv",
  2567.   "defivar", "defop", "defmethod", "deftypemethod", "deftp",
  2568.   "bad_type" };
  2569.  
  2570. int insertion_level = 0;
  2571. typedef struct istack_elt
  2572. {
  2573.   struct istack_elt *next;
  2574.   char *item_function;
  2575.   int line_number;
  2576.   int filling_enabled;
  2577.   int indented_fill;
  2578.   enum insertion_type insertion;
  2579.   int inhibited;
  2580. } INSERTION_ELT;
  2581.  
  2582. INSERTION_ELT *insertion_stack = (INSERTION_ELT *) NULL;
  2583.  
  2584. init_insertion_stack ()
  2585. {
  2586.   insertion_stack = (INSERTION_ELT *) NULL;
  2587. }
  2588.  
  2589. /* Return the type of the current insertion. */
  2590. enum insertion_type
  2591. current_insertion_type ()
  2592. {
  2593.   if (!insertion_level)
  2594.     return (bad_type);
  2595.   else
  2596.     return (insertion_stack->insertion);
  2597. }
  2598.  
  2599. /* Return a pointer to the string which is the function
  2600.    to wrap around items. */
  2601. char *
  2602. current_item_function ()
  2603. {
  2604.   if (!insertion_level)
  2605.     return ((char *) NULL);
  2606.   else
  2607.     return (insertion_stack->item_function);
  2608. }
  2609.  
  2610. char *
  2611. get_item_function ()
  2612. {
  2613.   char *item_function;
  2614.   get_rest_of_line (&item_function);
  2615.   backup_input_pointer ();
  2616.   canon_white (item_function);
  2617.   return (item_function);
  2618. }
  2619.  
  2620.  /* Push the state of the current insertion on the stack. */
  2621. push_insertion (type, item_function)
  2622.      enum insertion_type type;
  2623.      char *item_function;
  2624. {
  2625.   INSERTION_ELT *new = (INSERTION_ELT *) xmalloc (sizeof (INSERTION_ELT));
  2626.  
  2627.   new->item_function = item_function;
  2628.   new->filling_enabled = filling_enabled;
  2629.   new->indented_fill = indented_fill;
  2630.   new->insertion = type;
  2631.   new->line_number = line_number;
  2632.   new->inhibited = inhibit_paragraph_indentation;
  2633.   new->next = insertion_stack;
  2634.   insertion_stack = new;
  2635.   insertion_level++;
  2636. }
  2637.  
  2638.  /* Pop the value on top of the insertion stack into the
  2639.     global variables. */
  2640. pop_insertion ()
  2641. {
  2642.   INSERTION_ELT *temp = insertion_stack;
  2643.  
  2644.   if (temp == (INSERTION_ELT *) NULL)
  2645.     return;
  2646.  
  2647.   inhibit_paragraph_indentation = temp->inhibited;
  2648.   filling_enabled = temp->filling_enabled;
  2649.   indented_fill = temp->indented_fill;
  2650.   free_and_clear (&(temp->item_function));
  2651.   insertion_stack = insertion_stack->next;
  2652.   free (temp);
  2653.   insertion_level--;
  2654. }
  2655.  
  2656.  /* Return a pointer to the print name of this
  2657.     enumerated type. */
  2658. char *
  2659. insertion_type_pname (type)
  2660.      enum insertion_type type;
  2661. {
  2662.   if ((int) type < (int) bad_type)
  2663.     return (insertion_type_names[(int) type]);
  2664.   else
  2665.     return ("Broken-Type in insertion_type_pname");
  2666. }
  2667.  
  2668. /* Return the insertion_type associated with NAME.
  2669.    If the type is not one of the known ones, return BAD_TYPE. */
  2670. enum insertion_type
  2671. find_type_from_name (name)
  2672.      char *name;
  2673. {
  2674.   int index = 0;
  2675.   while (index < (int) bad_type)
  2676.     {
  2677.       if (stricmp (name, insertion_type_names[index]) == 0)
  2678.     return (enum insertion_type) index;
  2679.       index++;
  2680.     }
  2681.   return (bad_type);
  2682. }
  2683.  
  2684. do_nothing ()
  2685. {
  2686. }
  2687.  
  2688. int
  2689. defun_insertion (type)
  2690.      enum insertion_type type;
  2691. {
  2692.   return
  2693.     ((type == deffn)
  2694.      || (type == defun)
  2695.      || (type == defmac)
  2696.      || (type == defspec)
  2697.      || (type == defvr)
  2698.      || (type == defvar)
  2699.      || (type == defopt)
  2700.      || (type == deftypefn)
  2701.      || (type == deftypefun)
  2702.      || (type == deftypevr)
  2703.      || (type == deftypevar)
  2704.      || (type == defcv)
  2705.      || (type == defivar)
  2706.      || (type == defop)
  2707.      || (type == defmethod)
  2708.      || (type == deftypemethod)
  2709.      || (type == deftp));
  2710. }
  2711.  
  2712. /* MAX_NS is the maximum nesting level for enumerations.  I picked 100
  2713.    which seemed reasonable.  This doesn't control the number of items,
  2714.    just the number of nested lists. */
  2715. #define max_stack_depth 100
  2716. #define ENUM_DIGITS 1
  2717. #define ENUM_ALPHA  2
  2718. typedef struct {
  2719.   int enumtype;
  2720.   int enumval;
  2721. } DIGIT_ALPHA;
  2722.  
  2723. DIGIT_ALPHA enumstack[max_stack_depth];
  2724. int enumstack_offset = 0;
  2725. int current_enumval = 1;
  2726. int current_enumtype = ENUM_DIGITS;
  2727. char *enumeration_arg = (char *)NULL;
  2728.  
  2729. start_enumerating (at, type)
  2730.      int at, type;
  2731. {
  2732.   if ((enumstack_offset + 1) == max_stack_depth)
  2733.     {
  2734.       line_error ("Enumeration stack overflow");
  2735.       return;
  2736.     }
  2737.   enumstack[enumstack_offset].enumtype = current_enumtype;
  2738.   enumstack[enumstack_offset].enumval = current_enumval;
  2739.   enumstack_offset++;
  2740.   current_enumval = at;
  2741.   current_enumtype = type;
  2742. }
  2743.  
  2744. stop_enumerating ()
  2745. {
  2746.   --enumstack_offset;
  2747.   if (enumstack_offset < 0)
  2748.     enumstack_offset = 0;
  2749.  
  2750.   current_enumval = enumstack[enumstack_offset].enumval;
  2751.   current_enumtype = enumstack[enumstack_offset].enumtype;
  2752. }
  2753.  
  2754. /* Place a letter or digits into the output stream. */
  2755. enumerate_item ()
  2756. {
  2757.   char temp[10];
  2758.  
  2759.   if (current_enumtype == ENUM_ALPHA)
  2760.     {
  2761.       if (current_enumval == ('z' + 1) || current_enumval == ('Z' + 1))
  2762.     {
  2763.       current_enumval = ((current_enumval - 1) == 'z' ? 'a' : 'A');
  2764.       warning ("Lettering overflow, restarting at %c", current_enumval);
  2765.     }
  2766.       sprintf (temp, "%c. ", current_enumval);
  2767.     }
  2768.   else
  2769.     sprintf (temp, "%d. ", current_enumval);
  2770.  
  2771.   indent (output_column += (current_indent - strlen (temp)));
  2772.   add_word (temp);
  2773.   current_enumval++;
  2774. }
  2775.  
  2776. /* This is where the work for all the "insertion" style
  2777.    commands is done.  A huge switch statement handles the
  2778.    various setups, and generic code is on both sides. */
  2779. begin_insertion (type)
  2780.      enum insertion_type type;
  2781. {
  2782.   int no_discard = 0;
  2783.  
  2784.   if (defun_insertion (type))
  2785.     {
  2786.       push_insertion (type, savestring (""));
  2787.       no_discard++;
  2788.     }
  2789.   else
  2790.     push_insertion (type, get_item_function ());
  2791.  
  2792.   switch (type)
  2793.     {
  2794.     case menu:
  2795.       close_paragraph ();
  2796.  
  2797.       filling_enabled = no_indent = 0;
  2798.       inhibit_paragraph_indentation = 1;
  2799.  
  2800.       if (!no_headers)
  2801.     add_word ("* Menu:\n");
  2802.  
  2803.       in_menu++;
  2804.       no_discard++;
  2805.       break;
  2806.  
  2807.       /* I think @quotation is meant to do filling.
  2808.      If you don't want filling, then use @example. */
  2809.     case quotation:
  2810.       close_single_paragraph ();
  2811.       last_char_was_newline = no_indent = 0;
  2812.       indented_fill = filling_enabled = 1;
  2813.       inhibit_paragraph_indentation = 1;
  2814.       current_indent += default_indentation_increment;
  2815.       break;
  2816.  
  2817.     case display:
  2818.     case example:
  2819.     case smallexample:
  2820.     case lisp:
  2821.     case smalllisp:
  2822.       /* Just like @example, but no indentation. */
  2823.     case format:
  2824.  
  2825.       close_single_paragraph ();
  2826.       inhibit_paragraph_indentation = 1;
  2827.       in_fixed_width_font++;
  2828.       filling_enabled = 0;
  2829.       last_char_was_newline = 0;
  2830.  
  2831.       if (type != format)
  2832.     current_indent += default_indentation_increment;
  2833.  
  2834.       break;
  2835.  
  2836.     case table:
  2837.     case ftable:
  2838.     case vtable:
  2839.     case itemize:
  2840.       close_single_paragraph ();
  2841.       current_indent += default_indentation_increment;
  2842.       filling_enabled = indented_fill = 1;
  2843. #if defined (INDENT_PARAGRAPHS_IN_TABLE)
  2844.       inhibit_paragraph_indentation = 0;
  2845. #else
  2846.       inhibit_paragraph_indentation = 1;
  2847. #endif /* !INDENT_PARAGRAPHS_IN_TABLE */
  2848.  
  2849.       /* Make things work for losers who forget the itemize syntax. */
  2850.       if (type == itemize)
  2851.     {
  2852.       if (!(*insertion_stack->item_function))
  2853.         {
  2854.           free (insertion_stack->item_function);
  2855.           insertion_stack->item_function = savestring ("@bullet");
  2856.         }
  2857.     }
  2858.       break;
  2859.  
  2860.     case enumerate:
  2861.       close_single_paragraph ();
  2862.       inhibit_paragraph_indentation = no_indent = 0;
  2863.       current_indent += default_indentation_increment;
  2864.       filling_enabled = indented_fill = 1;
  2865.  
  2866.       if (isdigit (*enumeration_arg))
  2867.     start_enumerating (atoi (enumeration_arg), ENUM_DIGITS);
  2868.       else
  2869.     start_enumerating (*enumeration_arg, ENUM_ALPHA);
  2870.       break;
  2871.  
  2872.       /* Does nothing special in makeinfo. */
  2873.     case group:
  2874.       close_single_paragraph ();
  2875.       break;
  2876.  
  2877.       /* Insertions that are no-ops in info, but do something in TeX. */
  2878.     case ifinfo:
  2879.     case ifset:
  2880.     case ifclear:
  2881.     case cartouche:
  2882.       break;
  2883.  
  2884.     case deffn:
  2885.     case defun:
  2886.     case defmac:
  2887.     case defspec:
  2888.     case defvr:
  2889.     case defvar:
  2890.     case defopt:
  2891.     case deftypefn:
  2892.     case deftypefun:
  2893.     case deftypevr:
  2894.     case deftypevar:
  2895.     case defcv:
  2896.     case defivar:
  2897.     case defop:
  2898.     case defmethod:
  2899.     case deftypemethod:
  2900.     case deftp:
  2901.       inhibit_paragraph_indentation = 1;
  2902.       filling_enabled = indented_fill = 1;
  2903.       current_indent += default_indentation_increment;
  2904.       no_indent = 0;
  2905.       break;
  2906.  
  2907.     case flushleft:
  2908.       close_single_paragraph ();
  2909.       inhibit_paragraph_indentation = 1;
  2910.       filling_enabled = indented_fill = no_indent = 0;
  2911.       break;
  2912.  
  2913.     case flushright:
  2914.       close_single_paragraph ();
  2915.       filling_enabled = indented_fill = no_indent = 0;
  2916.       inhibit_paragraph_indentation = 1;
  2917.       force_flush_right++;
  2918.       break;
  2919.     }
  2920.  
  2921.   if (!no_discard)
  2922.     discard_until ("\n");
  2923. }
  2924.  
  2925. /* Try to end the insertion with the specified TYPE.
  2926.    TYPE, with a value of bad_type,  gets translated to match
  2927.    the value currently on top of the stack.
  2928.    Otherwise, if TYPE doesn't match the top of the insertion stack,
  2929.    give error. */
  2930. end_insertion (type)
  2931.      enum insertion_type type;
  2932. {
  2933.   enum insertion_type temp_type;
  2934.  
  2935.   if (!insertion_level)
  2936.     return;
  2937.  
  2938.   temp_type = current_insertion_type ();
  2939.  
  2940.   if (type == bad_type)
  2941.     type = temp_type;
  2942.  
  2943.   if (type != temp_type)
  2944.     {
  2945.       line_error
  2946.     ("Expected `%s', but saw `%s'.",
  2947.      insertion_type_pname (temp_type), insertion_type_pname (type));
  2948.       return;
  2949.     }
  2950.  
  2951.   pop_insertion ();
  2952.  
  2953.   switch (type)
  2954.     {
  2955.       /* "Insertions which have no effect on paragraph formatting. */
  2956.     case ifinfo:
  2957.     case ifset:
  2958.     case ifclear:
  2959.       break;
  2960.  
  2961.     case menu:
  2962.       in_menu--;        /* No longer hacking menus. */
  2963.       close_insertion_paragraph ();
  2964.       break;
  2965.  
  2966.     case enumerate:
  2967.       stop_enumerating ();
  2968.       close_insertion_paragraph ();
  2969.       current_indent -= default_indentation_increment;
  2970.       break;
  2971.  
  2972.     case flushleft:
  2973.     case group:
  2974.     case cartouche:
  2975.       close_insertion_paragraph ();
  2976.       break;
  2977.  
  2978.     case format:
  2979.     case display:
  2980.     case example:
  2981.     case smallexample:
  2982.     case lisp:
  2983.     case smalllisp:
  2984.     case quotation:
  2985.  
  2986.       /* @quotation is the only one of the above without a fixed width
  2987.      font. */
  2988.       if (type != quotation)
  2989.     in_fixed_width_font--;
  2990.  
  2991.       /* @format is the only fixed_width insertion without a change
  2992.      in indentation. */
  2993.       if (type != format)
  2994.     current_indent -= default_indentation_increment;
  2995.  
  2996.       /* The ending of one of these insertions always marks the
  2997.      start of a new paragraph. */
  2998.       close_insertion_paragraph ();
  2999.       break;
  3000.  
  3001.     case table:
  3002.     case ftable:
  3003.     case vtable:
  3004.     case itemize:
  3005.       current_indent -= default_indentation_increment;
  3006.       break;
  3007.  
  3008.     case flushright:
  3009.       force_flush_right--;
  3010.       close_insertion_paragraph ();
  3011.       break;
  3012.  
  3013.       /* Handle the @defun style insertions with a default clause. */
  3014.     default:
  3015.       current_indent -= default_indentation_increment;
  3016.       close_insertion_paragraph ();
  3017.       break;
  3018.     }
  3019. }
  3020.  
  3021. /* Insertions cannot cross certain boundaries, such as node beginnings.  In
  3022.    code that creates such boundaries, you should call discard_insertions ()
  3023.    before doing anything else.  It prints the errors for you, and cleans up
  3024.    the insertion stack. */
  3025. discard_insertions ()
  3026. {
  3027.   int real_line_number = line_number;
  3028.   while (insertion_stack)
  3029.     {
  3030.       if (insertion_stack->insertion == ifinfo ||
  3031.       insertion_stack->insertion == ifset ||
  3032.       insertion_stack->insertion == ifclear ||
  3033.       insertion_stack->insertion == cartouche)
  3034.     break;
  3035.       else
  3036.     {
  3037.       char *offender = (char *)
  3038.         insertion_type_pname (insertion_stack->insertion);
  3039.  
  3040.       line_number = insertion_stack->line_number;
  3041.       line_error ("This `%s' doesn't have a matching `%cend %s'", offender,
  3042.               COMMAND_PREFIX, offender);
  3043.       pop_insertion ();
  3044.     }
  3045.     }
  3046.   line_number = real_line_number;
  3047. }
  3048.  
  3049. /* The actual commands themselves. */
  3050.  
  3051. /* Commands which insert themselves. */
  3052. insert_self ()
  3053. {
  3054.   add_word (command);
  3055. }
  3056.  
  3057. /* Force line break */
  3058. cm_asterisk ()
  3059. {
  3060.   /* Force a line break in the output. */
  3061.   insert ('\n');
  3062.   indent (output_column = current_indent);
  3063. }
  3064.  
  3065. /* Insert ellipsis. */
  3066. cm_dots (arg)
  3067.      int arg;
  3068. {
  3069.   if (arg == START)
  3070.     add_word ("...");
  3071. }
  3072.  
  3073. cm_bullet (arg)
  3074.      int arg;
  3075. {
  3076.   if (arg == START)
  3077.     add_char ('*');
  3078. }
  3079.  
  3080. cm_minus (arg)
  3081.      int arg;
  3082. {
  3083.   if (arg == START)
  3084.     add_char ('-');
  3085. }
  3086.  
  3087. /* Insert "TeX". */
  3088. cm_TeX (arg)
  3089.      int arg;
  3090. {
  3091.   if (arg == START)
  3092.     add_word ("TeX");
  3093. }
  3094.  
  3095. cm_copyright (arg)
  3096.      int arg;
  3097. {
  3098.   if (arg == START)
  3099.     add_word ("(C)");
  3100. }
  3101.  
  3102. cm_today (arg)
  3103.      int arg;
  3104. {
  3105.   static char * months [12] =
  3106.     { "January", "February", "March", "April", "May", "June", "July",
  3107.     "August", "September", "October", "November", "December" };
  3108.   if (arg == START)
  3109.     {
  3110.       long timer = (time (0));
  3111.       struct tm * ts = (localtime (&timer));
  3112.       add_word_args
  3113.     ("%d %s %d",
  3114.      (ts -> tm_mday),
  3115.      (months [ts -> tm_mon]),
  3116.      ((ts -> tm_year) + 1900));
  3117.     }
  3118. }
  3119.  
  3120. cm_code (arg)
  3121.      int arg;
  3122. {
  3123.   extern int printing_index;
  3124.  
  3125.   if (printing_index)
  3126.     return;
  3127.  
  3128.   if (arg == START)
  3129.     {
  3130.       in_fixed_width_font++;
  3131.       add_char ('`');
  3132.     }
  3133.   else
  3134.     {
  3135.       add_word ("'");
  3136.       in_fixed_width_font--;
  3137.     }
  3138. }
  3139.  
  3140. cm_samp (arg)
  3141.      int arg;
  3142. {
  3143.   cm_code (arg);
  3144. }
  3145.  
  3146. cm_file (arg)
  3147.      int arg;
  3148. {
  3149.   cm_code (arg);
  3150. }
  3151.  
  3152. cm_kbd (arg)
  3153.      int arg;
  3154. {
  3155.   cm_code (arg);
  3156. }
  3157.  
  3158. cm_key (arg)
  3159.      int arg;
  3160. {
  3161. }
  3162.  
  3163. /* Convert the character at position into CTL. */
  3164. cm_ctrl (arg, position)
  3165.      int arg, position;
  3166. {
  3167.   if (arg == END)
  3168.     output_paragraph[position - 1] = CTL (output_paragraph[position]);
  3169. }
  3170.  
  3171. /* Small Caps in makeinfo just does all caps. */
  3172. cm_sc (arg, start_pos, end_pos)
  3173.      int arg, start_pos, end_pos;
  3174. {
  3175.   if (arg == END)
  3176.     {
  3177.       while (start_pos < end_pos)
  3178.     {
  3179.       output_paragraph[start_pos] =
  3180.         coerce_to_upper (output_paragraph[start_pos]);
  3181.       start_pos++;
  3182.     }
  3183.     }
  3184. }
  3185.  
  3186. /* @var in makeinfo just uppercases the text. */
  3187. cm_var (arg, start_pos, end_pos)
  3188.      int arg, start_pos, end_pos;
  3189. {
  3190.   if (arg == END)
  3191.     {
  3192.       while (start_pos < end_pos)
  3193.     {
  3194.       output_paragraph[start_pos] =
  3195.         coerce_to_upper (output_paragraph[start_pos]);
  3196.       start_pos++;
  3197.     }
  3198.     }
  3199. }
  3200.  
  3201. cm_dfn (arg, position)
  3202.      int arg, position;
  3203. {
  3204.   add_char ('"');
  3205. }
  3206.  
  3207. cm_emph (arg)
  3208.      int arg;
  3209. {
  3210.   add_char ('*');
  3211. }
  3212.  
  3213. cm_strong (arg, position)
  3214.      int arg, position;
  3215. {
  3216.   cm_emph (arg);
  3217. }
  3218.  
  3219. cm_cite (arg, position)
  3220.      int arg, position;
  3221. {
  3222.   if (arg == START)
  3223.     add_word ("`");
  3224.   else
  3225.     add_word ("'");
  3226. }
  3227.  
  3228. /* Current text is italicized. */
  3229. cm_italic (arg, start, end)
  3230.      int arg, start, end;
  3231. {
  3232. }
  3233.  
  3234. /* Current text is highlighted. */
  3235. cm_bold (arg, start, end)
  3236.      int arg, start, end;
  3237. {
  3238.   cm_italic (arg);
  3239. }
  3240.  
  3241. /* Current text is in roman font. */
  3242. cm_roman (arg, start, end)
  3243.      int arg, start, end;
  3244. {
  3245. }
  3246.  
  3247. /* Current text is in roman font. */
  3248. cm_titlefont (arg, start, end)
  3249.      int arg, start, end;
  3250. {
  3251. }
  3252.  
  3253. /* Italicize titles. */
  3254. cm_title (arg, start, end)
  3255.      int arg, start, end;
  3256. {
  3257.   cm_italic (arg);
  3258. }
  3259.  
  3260. /* @refill is a NOP. */
  3261. cm_refill ()
  3262. {
  3263. }
  3264.  
  3265. /* Prevent the argument from being split across two lines. */
  3266. cm_w (arg, start, end)
  3267.      int arg, start, end;
  3268. {
  3269.   if (arg == START)
  3270.     non_splitting_words++;
  3271.   else
  3272.     non_splitting_words--;
  3273. }
  3274.  
  3275.  
  3276. /* Explain that this command is obsolete, thus the user shouldn't
  3277.    do anything with it. */
  3278. cm_obsolete (arg, start, end)
  3279.      int arg, start, end;
  3280. {
  3281.   if (arg == START)
  3282.     warning ("The command `@%s' is obsolete", command);
  3283. }
  3284.  
  3285. /* Insert the text following input_text_offset up to the end of the line
  3286.    in a new, separate paragraph.  Directly underneath it, insert a
  3287.    line of WITH_CHAR, the same length of the inserted text. */
  3288. insert_and_underscore (with_char)
  3289.      int with_char;
  3290. {
  3291.   int len, i, old_no_indent;
  3292.   char *temp;
  3293.  
  3294.   close_paragraph ();
  3295.   filling_enabled =  indented_fill = 0;
  3296.   old_no_indent = no_indent;
  3297.   no_indent = 1;
  3298.   get_rest_of_line (&temp);
  3299.  
  3300.   len = output_position;
  3301.   execute_string ("%s\n", temp);
  3302.   free (temp);
  3303.  
  3304.   len = ((output_position + output_paragraph_offset) - 1) - len;
  3305.   for (i = 0; i < len; i++)
  3306.     add_char (with_char);
  3307.   insert ('\n');
  3308.   close_paragraph ();
  3309.   filling_enabled = 1;
  3310.   no_indent = old_no_indent;
  3311. }
  3312.  
  3313. /* The remainder of the text on this line is a chapter heading. */
  3314. cm_chapter ()
  3315. {
  3316.   insert_and_underscore ('*');
  3317. }
  3318.  
  3319. /* The remainder of the text on this line is a section heading. */
  3320. cm_section ()
  3321. {
  3322.   insert_and_underscore ('=');
  3323. }
  3324.  
  3325. /* The remainder of the text on this line is a subsection heading. */
  3326. cm_subsection ()
  3327. {
  3328.   insert_and_underscore ('-');
  3329. }
  3330.  
  3331. /* The remainder of the text on this line is a subsubsection heading. */
  3332. cm_subsubsection ()
  3333. {
  3334.   insert_and_underscore ('.');
  3335. }
  3336.  
  3337. /* Here is a structure which associates sectioning commands with
  3338.    an integer, hopefully to reflect the `depth' of the current
  3339.    section. */
  3340. struct {
  3341.   char *name;
  3342.   int level;
  3343. } section_alist[] = {
  3344.   { "unnumberedsubsubsec", 5 },
  3345.   { "unnumberedsubsec", 4 },
  3346.   { "unnumberedsec", 3 },
  3347.   { "unnumbered", 2 },
  3348.   { "appendixsubsubsec", 5 },
  3349.   { "appendixsubsec", 4 },
  3350.   { "appendixsec", 3 },
  3351.   { "appendixsection", 3 },
  3352.   { "appendix", 2 },
  3353.   { "subsubsec", 5 },
  3354.   { "subsubsection", 5 },
  3355.   { "subsection", 4 },
  3356.   { "section", 3 },
  3357.   { "chapter", 2 },
  3358.   { "top", 1 },
  3359.  
  3360.   { (char *)NULL, 0 }
  3361. };
  3362.  
  3363. /* Return an integer which identifies the type section present in TEXT. */
  3364. int
  3365. what_section (text)
  3366.      char *text;
  3367. {
  3368.   register int i, j;
  3369.   char *t;
  3370.  
  3371.  find_section_command:
  3372.   for (j = 0; text[j] && cr_or_whitespace (text[j]); j++);
  3373.   if (text[j] != '@')
  3374.     return (-1);
  3375.  
  3376.   text = text + j + 1;
  3377.  
  3378.   /* We skip @comment commands. */
  3379.   if ((strncmp (text, "comment", strlen ("comment")) == 0) ||
  3380.       (strncmp (text, "c ", strlen ("c ")) == 0))
  3381.     {
  3382.       while (*text++ != '\n');
  3383.       goto find_section_command;
  3384.     }
  3385.  
  3386.   /* Handle italicized sectioning commands. */
  3387.   if (*text == 'i')
  3388.     text++;
  3389.  
  3390.   for (j = 0; text[j] && !cr_or_whitespace (text[j]); j++);
  3391.  
  3392.   for (i = 0; t = section_alist[i].name; i++)
  3393.     {
  3394.       if (j == strlen (t) && strncmp (t, text, j) == 0)
  3395.     return (section_alist[i].level);
  3396.     }
  3397.   return (-1);
  3398. }
  3399.  
  3400. /* Treat this just like @unnumbered.  The only difference is
  3401.    in node defaulting. */
  3402. cm_top ()
  3403. {
  3404.   static int top_encountered = 0;
  3405.   cm_unnumbered ();
  3406.  
  3407.   /* It is an error to have more than one @top. */
  3408.   if (top_encountered)
  3409.     {
  3410.       TAG_ENTRY *tag = tag_table;
  3411.  
  3412.       line_error ("There already is a node having @top as a section");
  3413.  
  3414.       while (tag != (TAG_ENTRY *)NULL)
  3415.     {
  3416.       if ((tag->flags & IS_TOP))
  3417.         {
  3418.           int old_line_number = line_number;
  3419.           line_number = tag->line_no;
  3420.           line_error ("Here is the @top node.");
  3421.           line_number = old_line_number;
  3422.           return;
  3423.         }
  3424.       tag = tag->next_ent;
  3425.     }
  3426.     }
  3427.   else
  3428.     {
  3429.       top_encountered = 1;
  3430.  
  3431.       /* The most recently defined node is the top node. */
  3432.       if (tag_table)
  3433.     tag_table->flags |= IS_TOP;
  3434.  
  3435.       /* Now set the logical hierarchical level of the Top node. */
  3436.       {
  3437.     int orig_offset = input_text_offset;
  3438.  
  3439.     input_text_offset = search_forward ("\n@node", orig_offset);
  3440.  
  3441.     if (input_text_offset > 0)
  3442.       {
  3443.         int this_section;
  3444.  
  3445.         /* Move to the end of this line, and find out what the
  3446.            sectioning command is here. */
  3447.         while (input_text[input_text_offset] != '\n')
  3448.           input_text_offset++;
  3449.  
  3450.         if (input_text_offset < size_of_input_text)
  3451.           input_text_offset++;
  3452.  
  3453.         this_section = what_section (input_text + input_text_offset);
  3454.  
  3455.         /* If we found a sectioning command, then give the top section
  3456.            a level of this section - 1. */
  3457.         if (this_section != -1)
  3458.           {
  3459.         register int i;
  3460.  
  3461.         for (i = 0; section_alist[i].name; i++)
  3462.           if (strcmp (section_alist[i].name, "Top") == 0)
  3463.             {
  3464.               section_alist[i].level = this_section - 1;
  3465.               break;
  3466.             }
  3467.           }
  3468.       }
  3469.     input_text_offset = orig_offset;
  3470.       }
  3471.     }
  3472. }
  3473.  
  3474. /* The remainder of the text on this line is an unnumbered heading. */
  3475. cm_unnumbered ()
  3476. {
  3477.   cm_chapter ();
  3478. }
  3479.  
  3480. /* The remainder of the text on this line is an unnumbered section heading. */
  3481. cm_unnumberedsec ()
  3482. {
  3483.   cm_section ();
  3484. }
  3485.  
  3486. /* The remainder of the text on this line is an unnumbered
  3487.    subsection heading. */
  3488. cm_unnumberedsubsec ()
  3489. {
  3490.   cm_subsection ();
  3491. }
  3492.  
  3493. /* The remainder of the text on this line is an unnumbered
  3494.    subsubsection heading. */
  3495. cm_unnumberedsubsubsec ()
  3496. {
  3497.   cm_subsubsection ();
  3498. }
  3499.  
  3500. /* The remainder of the text on this line is an appendix heading. */
  3501. cm_appendix ()
  3502. {
  3503.   cm_chapter ();
  3504. }
  3505.  
  3506. /* The remainder of the text on this line is an appendix section heading. */
  3507. cm_appendixsec ()
  3508. {
  3509.   cm_section ();
  3510. }
  3511.  
  3512. /* The remainder of the text on this line is an appendix subsection heading. */
  3513. cm_appendixsubsec ()
  3514. {
  3515.   cm_subsection ();
  3516. }
  3517.  
  3518. /* The remainder of the text on this line is an appendix
  3519.    subsubsection heading. */
  3520. cm_appendixsubsubsec ()
  3521. {
  3522.   cm_subsubsection ();
  3523. }
  3524.  
  3525. /* Compatibility functions substitute for chapter, section, etc. */
  3526. cm_majorheading ()
  3527. {
  3528.   cm_chapheading ();
  3529. }
  3530.  
  3531. cm_chapheading ()
  3532. {
  3533.   cm_chapter ();
  3534. }
  3535.  
  3536. cm_heading ()
  3537. {
  3538.   cm_section ();
  3539. }
  3540.  
  3541. cm_subheading ()
  3542. {
  3543.   cm_subsection ();
  3544. }
  3545.  
  3546. cm_subsubheading ()
  3547. {
  3548.   cm_subsubsection ();
  3549. }
  3550.  
  3551.  
  3552. /* **************************************************************** */
  3553. /*                                    */
  3554. /*           Adding nodes, and making tags            */
  3555. /*                                    */
  3556. /* **************************************************************** */
  3557.  
  3558. /* Start a new tag table. */
  3559. init_tag_table ()
  3560. {
  3561.   while (tag_table != (TAG_ENTRY *) NULL)
  3562.     {
  3563.       TAG_ENTRY *temp = tag_table;
  3564.       free (temp->node);
  3565.       free (temp->prev);
  3566.       free (temp->next);
  3567.       free (temp->up);
  3568.       tag_table = tag_table->next_ent;
  3569.       free (temp);
  3570.     }
  3571. }
  3572.  
  3573. write_tag_table ()
  3574. {
  3575.   return (write_tag_table_internal (0));    /* Not indirect. */
  3576. }
  3577.  
  3578. write_tag_table_indirect ()
  3579. {
  3580.   return (write_tag_table_internal (1));
  3581. }
  3582.  
  3583. /* Write out the contents of the existing tag table.
  3584.    INDIRECT_P says how to format the output. */
  3585. write_tag_table_internal (indirect_p)
  3586.      int indirect_p;
  3587. {
  3588.   TAG_ENTRY *node = tag_table;
  3589.   int old_indent = no_indent;
  3590.  
  3591.   no_indent = 1;
  3592.   filling_enabled = 0;
  3593.   must_start_paragraph = 0;
  3594.   close_paragraph ();
  3595.  
  3596.   if (!indirect_p)
  3597.     {
  3598.       no_indent = 1;
  3599.       insert ('\n');
  3600.     }
  3601.  
  3602.   add_word_args ("\037\nTag Table:\n%s", indirect_p ? "(Indirect)\n" : "");
  3603.  
  3604.   while (node != (TAG_ENTRY *) NULL)
  3605.     {
  3606.       add_word_args ("Node: %s\177%d\n", node->node, node->position);
  3607.       node = node->next_ent;
  3608.     }
  3609.  
  3610.   add_word ("\037\nEnd Tag Table\n");
  3611.   flush_output ();
  3612.   no_indent = old_indent;
  3613. }
  3614.  
  3615. char *
  3616. get_node_token ()
  3617. {
  3618.   char *string;
  3619.  
  3620.   get_until_in_line (",", &string);
  3621.  
  3622.   if (curchar () == ',')
  3623.     input_text_offset++;
  3624.  
  3625.   canon_white (string);
  3626.  
  3627.   /* Allow things like @@nodename. */
  3628.   normalize_node_name (string);
  3629.  
  3630.   return (string);
  3631. }
  3632.  
  3633. /* Given a node name in STRING, remove double @ signs, replacing them
  3634.    with just one. */
  3635. normalize_node_name (string)
  3636.      char *string;
  3637. {
  3638.   register int i, l = strlen (string);
  3639.  
  3640.   for (i = 0; i < l; i++)
  3641.     {
  3642.       if (string[i] == '@' && string[i + 1] == '@')
  3643.     {
  3644.       strncpy (string + i, string + i + 1, l - i);
  3645.       l--;
  3646.     }
  3647.     }
  3648. }
  3649.  
  3650. /* Look up NAME in the tag table, and return the associated
  3651.    tag_entry.  If the node is not in the table return NULL. */
  3652. TAG_ENTRY *
  3653. find_node (name)
  3654.      char *name;
  3655. {
  3656.   TAG_ENTRY *tag = tag_table;
  3657.  
  3658.   while (tag != (TAG_ENTRY *) NULL)
  3659.     {
  3660.       if (stricmp (tag->node, name) == 0)
  3661.     return (tag);
  3662.       tag = tag->next_ent;
  3663.     }
  3664.   return ((TAG_ENTRY *) NULL);
  3665. }
  3666.  
  3667. /* Remember NODE and associates. */
  3668. remember_node (node, prev, next, up, position, line_no, no_warn)
  3669.      char *node, *prev, *next, *up;
  3670.      int position, line_no, no_warn;
  3671. {
  3672.   /* Check for existence of this tag already. */
  3673.   if (validating)
  3674.     {
  3675.       register TAG_ENTRY *tag = find_node (node);
  3676.       if (tag)
  3677.     {
  3678.       line_error ("Node `%s' multiply defined (%d is first definition)",
  3679.               node, tag->line_no);
  3680.       return;
  3681.     }
  3682.     }
  3683.  
  3684.   /* First, make this the current node. */
  3685.   current_node = node;
  3686.  
  3687.   /* Now add it to the list. */
  3688.   {
  3689.     TAG_ENTRY *new = (TAG_ENTRY *) xmalloc (sizeof (TAG_ENTRY));
  3690.     new->node = node;
  3691.     new->prev = prev;
  3692.     new->next = next;
  3693.     new->up = up;
  3694.     new->position = position;
  3695.     new->line_no = line_no;
  3696.     new->filename = node_filename;
  3697.     new->touched = 0;        /* not yet referenced. */
  3698.     new->flags = 0;
  3699.     if (no_warn)
  3700.       new->flags |= NO_WARN;
  3701.     new->next_ent = tag_table;
  3702.     tag_table = new;
  3703.   }
  3704. }
  3705.  
  3706.  
  3707. /* The order is: nodename, nextnode, prevnode, upnode.
  3708.    The next, prev, and up fields can be defaulted.
  3709.    You must follow a node command which has those fields
  3710.    defaulted with a sectioning command (e.g. @chapter) giving
  3711.    the "level" of that node.  It is an error not to do so.
  3712.    The defaults come from the menu in this nodes parent. */
  3713. cm_node ()
  3714. {
  3715.   char *node, *prev, *next, *up;
  3716.   int new_node_pos, defaulting, this_section, no_warn = 0;
  3717.   extern int already_outputting_pending_notes;
  3718.  
  3719.   if (strcmp (command, "nwnode") == 0)
  3720.     no_warn = 1;
  3721.  
  3722.   /* Get rid of unmatched brace arguments from previous commands. */
  3723.   discard_braces ();
  3724.  
  3725.   /* There also might be insertions left lying around that haven't been
  3726.      ended yet.  Do that also. */
  3727.   discard_insertions ();
  3728.  
  3729.   if (!already_outputting_pending_notes)
  3730.     {
  3731.       close_paragraph ();
  3732.       output_pending_notes ();
  3733.       free_pending_notes ();
  3734.     }
  3735.  
  3736.   filling_enabled = indented_fill = 0;
  3737.   new_node_pos = output_position + 1;
  3738.   current_footnote_number = 1;
  3739.  
  3740.   node = get_node_token ();
  3741.   next = get_node_token ();
  3742.   prev = get_node_token ();
  3743.   up = get_node_token ();
  3744.  
  3745.   no_indent = 1;
  3746.   if (!no_headers)
  3747.     add_word_args ("\037\nFile: %s,  Node: %s", pretty_output_filename, node);
  3748.  
  3749.   /* Check for defaulting of this node's next, prev, and up fields. */
  3750.   defaulting = ((strlen (next) == 0) &&
  3751.         (strlen (prev) == 0) &&
  3752.         (strlen (up) == 0));
  3753.  
  3754.   this_section = what_section (input_text + input_text_offset);
  3755.  
  3756.   /* If we are defaulting, then look at the immediately following
  3757.      sectioning command (error if none) to determine the node's
  3758.      level.  Find the node that contains the menu mentioning this node
  3759.      that is one level up (error if not found).  That node is the "Up"
  3760.      of this node.  Default the "Next" and "Prev" from the menu. */
  3761.   if (defaulting)
  3762.     {
  3763.       NODE_REF *last_ref = (NODE_REF *)NULL;
  3764.       NODE_REF *ref = node_references;
  3765.  
  3766.       if (this_section < 0)
  3767.     {
  3768.       char *polite_section_name = "top";
  3769.       int i;
  3770.  
  3771.       for (i = 0; section_alist[i].name; i++)
  3772.         if (section_alist[i].level == current_section + 1)
  3773.           {
  3774.         polite_section_name = section_alist[i].name;
  3775.         break;
  3776.           }
  3777.  
  3778.       line_error
  3779.         ("Node `%s' requires a sectioning command (e.g. @%s)",
  3780.          node, polite_section_name);
  3781.     }
  3782.       else
  3783.     {
  3784.       if (stricmp (node, "Top") == 0)
  3785.         {
  3786.           /* Default the NEXT pointer to be the first menu item in
  3787.          this node, if there is a menu in this node. */
  3788.           {
  3789.         int orig_offset, orig_size;
  3790.         char *glean_node_from_menu ();
  3791.  
  3792.         orig_offset = input_text_offset;
  3793.         orig_size = search_forward ("\n@node ", orig_offset);
  3794.  
  3795.         if (orig_size < 0)
  3796.           orig_size = size_of_input_text;
  3797.  
  3798.         input_text_offset = search_forward ("\n@menu", orig_offset);
  3799.         if (input_text_offset > -1)
  3800.           {
  3801.             input_text_offset =
  3802.               search_forward ("\n* ", input_text_offset);
  3803.  
  3804.             if (input_text_offset > -1)
  3805.               next = glean_node_from_menu (0);
  3806.  
  3807.             if (next)
  3808.               {
  3809.             prev = savestring ("(DIR)");
  3810.             up = savestring ("(DIR)");
  3811.               }
  3812.           }
  3813.         input_text_offset = orig_offset;
  3814.           }
  3815.         }
  3816.  
  3817.       while (ref)
  3818.         {
  3819.           if (ref->section == (this_section - 1) &&
  3820.           ref->type == menu_reference &&
  3821.           stricmp (ref->node, node) == 0)
  3822.         {
  3823.           char *containing_node = ref->containing_node;
  3824.  
  3825.           free (up);
  3826.           up = savestring (containing_node);
  3827.  
  3828.           if (last_ref &&
  3829.               strcmp
  3830.               (last_ref->containing_node, containing_node) == 0)
  3831.             {
  3832.               free (next);
  3833.               next = savestring (last_ref->node);
  3834.             }
  3835.  
  3836.           while ((ref->section == this_section - 1) &&
  3837.              (ref->next) &&
  3838.              (ref->next->type != menu_reference))
  3839.             ref = ref->next;
  3840.  
  3841.           if (ref->next &&
  3842.               strcmp
  3843.               (ref->next->containing_node, containing_node) == 0)
  3844.             {
  3845.               free (prev);
  3846.               prev = savestring (ref->next->node);
  3847.             }
  3848.           else if (!ref->next &&
  3849.                stricmp (ref->containing_node, "Top") == 0)
  3850.             {
  3851.               free (prev);
  3852.               prev = savestring (ref->containing_node);
  3853.             }
  3854.           break;
  3855.         }
  3856.           last_ref = ref;
  3857.           ref = ref->next;
  3858.         }
  3859.     }
  3860.     }
  3861.  
  3862.   if (!no_headers)
  3863.     {
  3864.       if (*next)
  3865.     add_word_args (",  Next: %s", next);
  3866.  
  3867.       if (*prev)
  3868.     add_word_args (",  Prev: %s", prev);
  3869.  
  3870.       if (*up)
  3871.     add_word_args (",  Up: %s", up);
  3872.     }
  3873.  
  3874.   insert ('\n');
  3875.   close_paragraph ();
  3876.   no_indent = 0;
  3877.  
  3878.   if (!*node)
  3879.     {
  3880.       line_error ("No node name specified for `@%s' command", command);
  3881.       free (node);
  3882.       free (next);
  3883.       free (prev);
  3884.       free (up);
  3885.     }
  3886.   else
  3887.     {
  3888.       if (!*next) { free (next); next = (char *)NULL; }
  3889.       if (!*prev) { free (prev); prev = (char *)NULL; }
  3890.       if (!*up) { free (up); up = (char *)NULL; }
  3891.       remember_node (node, prev, next, up, new_node_pos, line_number, no_warn);
  3892.     }
  3893.  
  3894.   /* Change the section only if there was a sectioning command. */
  3895.   if (this_section >= 0)
  3896.     current_section = this_section;
  3897.  
  3898.   filling_enabled = 1;
  3899. }
  3900.  
  3901. /* Validation of an info file.
  3902.    Scan through the list of tag entrys touching the Prev, Next, and Up
  3903.    elements of each.  It is an error not to be able to touch one of them,
  3904.    except in the case of external node references, such as "(DIR)".
  3905.  
  3906.    If the Prev is different from the Up,
  3907.    then the Prev node must have a Next pointing at this node.
  3908.  
  3909.    Every node except Top must have an Up.
  3910.    The Up node must contain some sort of reference, other than a Next,
  3911.    to this node.
  3912.  
  3913.    If the Next is different from the Next of the Up,
  3914.    then the Next node must have a Prev pointing at this node. */
  3915. validate_file (filename, tag_table)
  3916.      char *filename;
  3917.      TAG_ENTRY *tag_table;
  3918. {
  3919.   char *old_input_filename = input_filename;
  3920.   TAG_ENTRY *tags = tag_table;
  3921.  
  3922.   while (tags != (TAG_ENTRY *) NULL)
  3923.     {
  3924.       register TAG_ENTRY *temp_tag;
  3925.  
  3926.       input_filename = tags->filename;
  3927.       line_number = tags->line_no;
  3928.  
  3929.       /* If this node has a Next, then make sure that the Next exists. */
  3930.       if (tags->next)
  3931.     {
  3932.       validate (tags->next, tags->line_no, "Next");
  3933.  
  3934.       /* If the Next node exists, and there is no Up, then make
  3935.          sure that the Prev of the Next points back. */
  3936.       if (temp_tag = find_node (tags->next))
  3937.         {
  3938.           char *prev = temp_tag->prev;
  3939.           if (!prev || (stricmp (prev, tags->node) != 0))
  3940.         {
  3941.           line_error
  3942.             ("Node `%s''s Next field not pointed back to", tags->node);
  3943.           line_number = temp_tag->line_no;
  3944.           input_filename = temp_tag->filename;
  3945.           line_error
  3946.             ("This node (`%s') is the one with the bad `Prev'",
  3947.              temp_tag->node);
  3948.           input_filename = tags->filename;
  3949.           line_number = tags->line_no;
  3950.           temp_tag->flags |= PREV_ERROR;
  3951.         }
  3952.         }
  3953.     }
  3954.  
  3955.       /* Validate the Prev field if there is one, and we haven't already
  3956.      complained about it in some way.  You don't have to have a Prev
  3957.      field at this stage. */
  3958.       if (!(tags->flags & PREV_ERROR) && tags->prev)
  3959.     {
  3960.       int valid = validate (tags->prev, tags->line_no, "Prev");
  3961.  
  3962.       if (!valid)
  3963.         tags->flags |= PREV_ERROR;
  3964.       else
  3965.         {
  3966.           /* If the Prev field is not the same as the Up field,
  3967.          then the node pointed to by the Prev field must have
  3968.          a Next field which points to this node. */
  3969.           if (tags->up && (stricmp (tags->prev, tags->up) != 0))
  3970.         {
  3971.           temp_tag = find_node (tags->prev);
  3972.           if (!temp_tag->next ||
  3973.               (stricmp (temp_tag->next, tags->node) != 0))
  3974.             {
  3975.               line_error ("Node `%s''s Prev field not pointed back to",
  3976.                   tags->node);
  3977.               line_number = temp_tag->line_no;
  3978.               input_filename = temp_tag->filename;
  3979.               line_error
  3980.             ("This node (`%s') is the one with the bad `Next'",
  3981.              temp_tag->node);
  3982.               input_filename = tags->filename;
  3983.               line_number = tags->line_no;
  3984.               temp_tag->flags |= NEXT_ERROR;
  3985.             }
  3986.         }
  3987.         }
  3988.     }
  3989.  
  3990.       if (!tags->up && (stricmp (tags->node, "Top") != 0))
  3991.     line_error ("Node `%s' is missing an \"Up\" field", tags->node);
  3992.       else if (tags->up)
  3993.     {
  3994.       int valid = validate (tags->up, tags->line_no, "Up");
  3995.  
  3996.       /* If node X has Up: Y, then warn if Y fails to have a menu item
  3997.          or note pointing at X, if Y isn't of the form "(Y)". */
  3998.       if (valid && *tags->up != '(')
  3999.         {
  4000.           NODE_REF *nref, *tref, *list;
  4001.           NODE_REF *find_node_reference ();
  4002.  
  4003.           tref = (NODE_REF *) NULL;
  4004.           list = node_references;
  4005.  
  4006.           for (;;)
  4007.         {
  4008.           if (!(nref = find_node_reference (tags->node, list)))
  4009.             break;
  4010.  
  4011.           if (stricmp (nref->containing_node, tags->up) == 0)
  4012.             {
  4013.               if (nref->type != menu_reference)
  4014.             {
  4015.               tref = nref;
  4016.               list = nref->next;
  4017.             }
  4018.               else
  4019.             break;
  4020.             }
  4021.           list = nref->next;
  4022.         }
  4023.  
  4024.           if (!nref)
  4025.         {
  4026.           temp_tag = find_node (tags->up);
  4027.           line_number = temp_tag->line_no;
  4028.           filename = temp_tag->filename;
  4029.           if (!tref)
  4030.             line_error (
  4031. "`%s' has an Up field of `%s', but `%s' has no menu item for `%s'",
  4032.                 tags->node, tags->up, tags->up, tags->node);
  4033.           line_number = tags->line_no;
  4034.           filename = tags->filename;
  4035.         }
  4036.         }
  4037.     }
  4038.       tags = tags->next_ent;
  4039.     }
  4040.  
  4041.   validate_other_references (node_references);
  4042.   /* We have told the user about the references which didn't exist.
  4043.      Now tell him about the nodes which aren't referenced. */
  4044.  
  4045.   tags = tag_table;
  4046.   while (tags != (TAG_ENTRY *) NULL)
  4047.     {
  4048.       /* Special hack.  If the node in question appears to have
  4049.          been referenced more than REFERENCE_WARNING_LIMIT times,
  4050.          give a warning. */
  4051.       if (tags->touched > reference_warning_limit)
  4052.     {
  4053.       input_filename = tags->filename;
  4054.       line_number = tags->line_no;
  4055.       warning ("Node `%s' has been referenced %d times",
  4056.            tags->node, tags->touched);
  4057.     }
  4058.  
  4059.       if (tags->touched == 0)
  4060.     {
  4061.       input_filename = tags->filename;
  4062.       line_number = tags->line_no;
  4063.  
  4064.       /* Notice that the node "Top" is special, and doesn't have to
  4065.          be referenced. */
  4066.       if (stricmp (tags->node, "Top") != 0)
  4067.         warning ("Unreferenced node `%s'", tags->node);
  4068.     }
  4069.       tags = tags->next_ent;
  4070.     }
  4071.   input_filename = old_input_filename;
  4072. }
  4073.  
  4074. /* Return 1 if tag correctly validated, or 0 if not. */
  4075. validate (tag, line, label)
  4076.      char *tag;
  4077.      int line;
  4078.      char *label;
  4079. {
  4080.   TAG_ENTRY *result;
  4081.  
  4082.   /* If there isn't a tag to verify, or if the tag is in another file,
  4083.      then it must be okay. */
  4084.   if (!tag || !*tag || *tag == '(')
  4085.     return (1);
  4086.  
  4087.   /* Otherwise, the tag must exist. */
  4088.   result = find_node (tag);
  4089.  
  4090.   if (!result)
  4091.     {
  4092.       line_number = line;
  4093.       line_error (
  4094. "Validation error.  `%s' field points to node `%s', which doesn't exist",
  4095.           label, tag);
  4096.       return (0);
  4097.     }
  4098.   result->touched++;
  4099.   return (1);
  4100. }
  4101.  
  4102. /* Split large output files into a series of smaller files.  Each file
  4103.    is pointed to in the tag table, which then gets written out as the
  4104.    original file.  The new files have the same name as the original file
  4105.    with a "-num" attached.  SIZE is the largest number of bytes to allow
  4106.    in any single split file. */
  4107. split_file (filename, size)
  4108.      char *filename;
  4109.      int size;
  4110. {
  4111.   char *root_filename, *root_pathname;
  4112.   char *the_file, *filename_part ();
  4113.   struct stat fileinfo;
  4114.   char *the_header;
  4115.   int header_size;
  4116.  
  4117.   /* Can only do this to files with tag tables. */
  4118.   if (!tag_table)
  4119.     return;
  4120.  
  4121.   if (size == 0)
  4122.     size = DEFAULT_SPLIT_SIZE;
  4123.  
  4124.   if (output_stream)
  4125. {  
  4126.     fclose(output_stream);
  4127.     output_stream = NULL;
  4128. }
  4129.   if ((stat (filename, &fileinfo) != 0) ||
  4130.       (fileinfo.st_size < SPLIT_SIZE_THRESHOLD))
  4131.     return;
  4132.  
  4133.   the_file = find_and_load (filename);
  4134.   if (!the_file)
  4135.     return;
  4136.  
  4137.   root_filename = filename_part (filename);
  4138.   root_pathname = pathname_part (filename);
  4139.  
  4140.   if (!root_pathname)
  4141.     root_pathname = savestring ("");
  4142.  
  4143.   /* Start splitting the file.  Walk along the tag table
  4144.      outputting sections of the file.  When we have written
  4145.      all of the nodes in the tag table, make the top-level
  4146.      pointer file, which contains indirect pointers and
  4147.      tags for the nodes. */
  4148.   {
  4149.     int which_file = 1;
  4150.     TAG_ENTRY *tags = tag_table;
  4151.     char *indirect_info = (char *)NULL;
  4152.  
  4153.     /* Remember the `header' of this file.  The first tag in the file is
  4154.        the bottom of the header; the top of the file is the start. */
  4155.     the_header = (char *)xmalloc (1 + (header_size = (tags->position - 2)));
  4156.     bcopy (the_file, the_header, header_size);
  4157.  
  4158.     while (tags)
  4159.       {
  4160.     int file_top, file_bot, limit;
  4161.  
  4162.     /* Have to include the Control-_. */
  4163.     file_top = file_bot = tags->position - 2;
  4164.     limit = file_top + size;
  4165.  
  4166.     /* If the rest of this file is only one node, then
  4167.        that is the entire subfile. */
  4168.     if (!tags->next_ent)
  4169.       {
  4170.         int i = tags->position + 1;
  4171.         char last_char = the_file[i];
  4172.  
  4173.         while (i < current_file_size)
  4174.           {
  4175.         if ((the_file[i] == '\037') &&
  4176.             ((last_char == '\n') ||
  4177.              (last_char == '\014')))
  4178.           break;
  4179.         else
  4180.           last_char = the_file[i];
  4181.         i++;
  4182.           }
  4183.         file_bot = i;
  4184.         tags = tags->next_ent;
  4185.         goto write_region;
  4186.       }
  4187.  
  4188.     /* Otherwise, find the largest number of nodes that can fit in
  4189.        this subfile. */
  4190.     for (; tags; tags = tags->next_ent)
  4191.       {
  4192.         if (!tags->next_ent)
  4193.           {
  4194.         /* This entry is the last node.  Search forward for the end
  4195.                of this node, and that is the end of this file. */
  4196.         int i = tags->position + 1;
  4197.         char last_char = the_file[i];
  4198.  
  4199.         while (i < current_file_size)
  4200.           {
  4201.             if ((the_file[i] == '\037') &&
  4202.             ((last_char == '\n') ||
  4203.              (last_char == '\014')))
  4204.               break;
  4205.             else
  4206.               last_char = the_file[i];
  4207.             i++;
  4208.           }
  4209.         file_bot = i;
  4210.  
  4211.         if (file_bot < limit)
  4212.           {
  4213.             tags = tags->next_ent;
  4214.             goto write_region;
  4215.           }
  4216.         else
  4217.           {
  4218.             /* Here we want to write out everything before the last
  4219.                node, and then write the last node out in a file
  4220.                by itself. */
  4221.             file_bot = tags->position;
  4222.             goto write_region;
  4223.           }
  4224.           }
  4225.  
  4226.         if (tags->next_ent->position > limit)
  4227.           {
  4228.         if ((tags->position) - 2 == file_top)
  4229.           tags = tags->next_ent;
  4230.         file_bot = tags->position;
  4231.           write_region:
  4232.         {
  4233.           int fd;
  4234.           char *stp;
  4235.           char *split_file = (char *)
  4236.             xmalloc (10
  4237.                  + strlen (root_pathname)
  4238.                  + strlen (root_filename));
  4239.           sprintf (split_file, "%s%s", root_pathname, root_filename);
  4240.           if (!(stp = strrchr(split_file, '.')))
  4241.               stp = split_file + strlen(split_file);
  4242.           sprintf (stp, ".i%02d", which_file);
  4243.  
  4244.           if (((fd = open (split_file, O_WRONLY | O_TRUNC | O_CREAT, 0666)) < 0)
  4245.               || (write (fd, the_header, header_size) != header_size)
  4246.               || (write (fd, the_file + file_top, file_bot - file_top)
  4247.               != (file_bot - file_top))
  4248.               || ((close (fd)) < 0))
  4249.             {
  4250.               perror (split_file);
  4251.               close (fd);
  4252.               exit (FATAL);
  4253.             }
  4254.  
  4255.           if (!indirect_info)
  4256.             {
  4257.               indirect_info = the_file + file_top;
  4258.               sprintf (indirect_info, "\037\nIndirect:\n");
  4259.               indirect_info += strlen (indirect_info);
  4260.             }
  4261.  
  4262.           sprintf (split_file, "%s", root_filename);
  4263.           if (!(stp = strrchr(split_file, '.')))
  4264.               stp = split_file + strlen(split_file);
  4265.           sprintf (stp, ".i%02d", which_file);
  4266.  
  4267.           sprintf (indirect_info, "%s: %d\n",
  4268.                split_file, file_top);
  4269.  
  4270.           free (split_file);
  4271.           indirect_info += strlen (indirect_info);
  4272.           which_file++;
  4273.           break;
  4274.         }
  4275.           }
  4276.       }
  4277.       }
  4278.  
  4279.     /* We have sucessfully created the subfiles.  Now write out the
  4280.        original again.  We must use `output_stream', or
  4281.        write_tag_table_indirect () won't know where to place the output. */
  4282.     output_stream = fopen (filename, "w");
  4283.     current_file_size = 0;
  4284.     if (!output_stream)
  4285.       {
  4286.     perror (filename);
  4287.     exit (FATAL);
  4288.       }
  4289.  
  4290.     {
  4291.       int distance = indirect_info - the_file;
  4292.       fwrite (the_file, 1, distance, output_stream);
  4293.  
  4294.       /* Inhibit newlines. */
  4295.       paragraph_is_open = 0;
  4296.  
  4297.       write_tag_table_indirect ();
  4298.       fclose (output_stream);
  4299.       free (the_header);
  4300.       free (the_file);
  4301.       return;
  4302.     }
  4303.   }
  4304. }
  4305.  
  4306. /* Some menu hacking.  This is used to remember menu references while
  4307.    reading the input file.  After the output file has been written, if
  4308.    validation is on, then we use the contents of NODE_REFERENCES as a
  4309.    list of nodes to validate. */
  4310. char *
  4311. reftype_type_string (type)
  4312.      enum reftype type;
  4313. {
  4314.   switch (type)
  4315.     {
  4316.     case menu_reference:
  4317.       return ("Menu");
  4318.     case followed_reference:
  4319.       return ("Followed-Reference");
  4320.     default:
  4321.       return ("Internal-bad-reference-type");
  4322.     }
  4323. }
  4324.  
  4325. /* Remember this node name for later validation use. */
  4326. remember_node_reference (node, line, type)
  4327.      char *node;
  4328.      int line;
  4329.      enum reftype type;
  4330. {
  4331.   NODE_REF *temp = (NODE_REF *) xmalloc (sizeof (NODE_REF));
  4332.  
  4333.   temp->next = node_references;
  4334.   temp->node = savestring (node);
  4335.   temp->line_no = line;
  4336.   temp->section = current_section;
  4337.   temp->type = type;
  4338.   temp->containing_node = savestring (current_node);
  4339.   temp->filename = node_filename;
  4340.  
  4341.   node_references = temp;
  4342. }
  4343.  
  4344. validate_other_references (ref_list)
  4345.      register NODE_REF *ref_list;
  4346. {
  4347.   char *old_input_filename = input_filename;
  4348.  
  4349.   while (ref_list != (NODE_REF *) NULL)
  4350.     {
  4351.       input_filename = ref_list->filename;
  4352.       validate (ref_list->node, ref_list->line_no,
  4353.         reftype_type_string (ref_list->type));
  4354.       ref_list = ref_list->next;
  4355.     }
  4356.   input_filename = old_input_filename;
  4357. }
  4358.  
  4359. /* Find NODE in REF_LIST. */
  4360. NODE_REF *
  4361. find_node_reference (node, ref_list)
  4362.      char *node;
  4363.      register NODE_REF *ref_list;
  4364. {
  4365.   while (ref_list)
  4366.     {
  4367.       if (stricmp (node, ref_list->node) == 0)
  4368.     break;
  4369.       ref_list = ref_list->next;
  4370.     }
  4371.   return (ref_list);
  4372. }
  4373.  
  4374. free_node_references ()
  4375. {
  4376.   register NODE_REF *list, *temp;
  4377.  
  4378.   list = node_references;
  4379.  
  4380.   while (list)
  4381.     {
  4382.       temp = list;
  4383.       free (list->node);
  4384.       free (list->containing_node);
  4385.       list = list->next;
  4386.       free (temp);
  4387.     }
  4388.   node_references = (NODE_REF *) NULL;
  4389. }
  4390.  
  4391.   /* This function gets called at the start of every line while inside of
  4392.      a menu.  It checks to see if the line starts with "* ", and if so,
  4393.      remembers the node reference that this menu refers to.
  4394.      input_text_offset is at the \n just before the line start. */
  4395. #define menu_starter "* "
  4396. char *
  4397. glean_node_from_menu (remember_reference)
  4398.      int remember_reference;
  4399. {
  4400.   int i, orig_offset = input_text_offset;
  4401.   char *nodename;
  4402.  
  4403.   if (strncmp (&input_text[input_text_offset + 1],
  4404.            menu_starter,
  4405.            strlen (menu_starter)) != 0)
  4406.     return ((char *)NULL);
  4407.   else
  4408.     input_text_offset += strlen (menu_starter) + 1;
  4409.  
  4410.   get_until_in_line (":", &nodename);
  4411.   if (curchar () == ':')
  4412.     input_text_offset++;
  4413.   canon_white (nodename);
  4414.  
  4415.   if (curchar () == ':')
  4416.     goto save_node;
  4417.  
  4418.   free (nodename);
  4419.   get_rest_of_line (&nodename);
  4420.  
  4421.   /* Special hack: If the nodename follows the menu item name,
  4422.      then we have to read the rest of the line in order to find
  4423.      out what the nodename is.  But we still have to read the
  4424.      line later, in order to process any formatting commands that
  4425.      might be present.  So un-count the carriage return that has just
  4426.      been counted. */
  4427.   line_number--;
  4428.  
  4429.   canon_white (nodename);
  4430.   for (i = 0; i < strlen (nodename); i++)
  4431.     {
  4432.       if (nodename[i] == '\t' ||
  4433.       nodename[i] == '.' ||
  4434.       nodename[i] == ',')
  4435.     {
  4436.       nodename[i] = '\0';
  4437.       break;
  4438.     }
  4439.     }
  4440. save_node:
  4441.   input_text_offset = orig_offset;
  4442.   normalize_node_name (nodename);
  4443.   i = strlen (nodename);
  4444.   if (i && nodename[i - 1] == ':')
  4445.     nodename[i - 1] = '\0';
  4446.  
  4447.   if (remember_reference)
  4448.     {
  4449.       remember_node_reference (nodename, line_number, menu_reference);
  4450.       free (nodename);
  4451.       return ((char *)NULL);
  4452.     }
  4453.   else
  4454.     return (nodename);
  4455. }
  4456.  
  4457. cm_menu ()
  4458. {
  4459.   begin_insertion (menu);
  4460. }
  4461.  
  4462.  
  4463. /* **************************************************************** */
  4464. /*                                    */
  4465. /*            Cross Reference Hacking                */
  4466. /*                                    */
  4467. /* **************************************************************** */
  4468.  
  4469. char *
  4470. get_xref_token ()
  4471. {
  4472.   char *string;
  4473.  
  4474.   get_until_in_braces (",", &string);
  4475.   if (curchar () == ',')
  4476.     input_text_offset++;
  4477.   fix_whitespace (string);
  4478.   return (string);
  4479. }
  4480.  
  4481. int px_ref_flag = 0;        /* Controls initial output string. */
  4482.  
  4483. /* Make a cross reference. */
  4484. cm_xref (arg)
  4485. {
  4486.   if (arg == START)
  4487.     {
  4488.       char *arg1, *arg2, *arg3, *arg4, *arg5;
  4489.  
  4490.       arg1 = get_xref_token ();
  4491.       arg2 = get_xref_token ();
  4492.       arg3 = get_xref_token ();
  4493.       arg4 = get_xref_token ();
  4494.       arg5 = get_xref_token ();
  4495.  
  4496.       add_word_args ("%s", px_ref_flag ? "*note " : "*Note ");
  4497.  
  4498.       if (*arg5 || *arg4)
  4499.     {
  4500.       char *node_name;
  4501.  
  4502.       if (!*arg2)
  4503.         {
  4504.           if (*arg3)
  4505.         node_name = arg3;
  4506.           else
  4507.         node_name = arg1;
  4508.         }
  4509.       else
  4510.         node_name = arg2;
  4511.  
  4512.       execute_string ("%s: (%s)%s", node_name, arg4, arg1);
  4513.       return;
  4514.     }
  4515.       else
  4516.     remember_node_reference (arg1, line_number, followed_reference);
  4517.  
  4518.       if (*arg3)
  4519.     {
  4520.       if (!*arg2)
  4521.         execute_string ("%s: %s", arg3, arg1);
  4522.       else
  4523.         execute_string ("%s: %s", arg2, arg1);
  4524.       return;
  4525.     }
  4526.  
  4527.       if (*arg2)
  4528.     execute_string ("%s: %s", arg2, arg1);
  4529.       else
  4530.     execute_string ("%s::", arg1);
  4531.     }
  4532.   else
  4533.     {
  4534.  
  4535.       /* Check to make sure that the next non-whitespace character is either
  4536.          a period or a comma. input_text_offset is pointing at the "}" which
  4537.          ended the xref or pxref command. */
  4538.       int temp = input_text_offset + 1;
  4539.  
  4540.       if (output_paragraph[output_paragraph_offset - 2] == ':' &&
  4541.       output_paragraph[output_paragraph_offset - 1] == ':')
  4542.     return;
  4543.       while (temp < size_of_input_text)
  4544.     {
  4545.       if (cr_or_whitespace (input_text[temp]))
  4546.         temp++;
  4547.       else
  4548.         {
  4549.           if (input_text[temp] == '.' ||
  4550.           input_text[temp] == ',' ||
  4551.           input_text[temp] == '\t')
  4552.         return;
  4553.           else
  4554.         {
  4555.           line_error ("Cross-reference must be terminated with a period or a comma");
  4556.           return;
  4557.         }
  4558.         }
  4559.     }
  4560.     }
  4561. }
  4562.  
  4563. cm_pxref (arg)
  4564.      int arg;
  4565. {
  4566.   if (arg == START)
  4567.     {
  4568.       px_ref_flag++;
  4569.       cm_xref (arg);
  4570.       px_ref_flag--;
  4571.     }
  4572.   else
  4573.     add_char ('.');
  4574. }
  4575.  
  4576. cm_inforef (arg)
  4577.      int arg;
  4578. {
  4579.   if (arg == START)
  4580.     {
  4581.       char *node, *pname, *file;
  4582.  
  4583.       node = get_xref_token ();
  4584.       pname = get_xref_token ();
  4585.       file = get_xref_token ();
  4586.  
  4587.       execute_string ("*note %s: (%s)%s", pname, file, node);
  4588.     }
  4589. }
  4590.  
  4591. /* **************************************************************** */
  4592. /*                                    */
  4593. /*            Insertion Command Stubs                */
  4594. /*                                    */
  4595. /* **************************************************************** */
  4596.  
  4597. cm_quotation ()
  4598. {
  4599.   begin_insertion (quotation);
  4600. }
  4601.  
  4602. cm_example ()
  4603. {
  4604.   begin_insertion (example);
  4605. }
  4606.  
  4607. cm_smallexample ()
  4608. {
  4609.   begin_insertion (smallexample);
  4610. }
  4611.  
  4612. cm_lisp ()
  4613. {
  4614.   begin_insertion (lisp);
  4615. }
  4616.  
  4617. cm_smalllisp ()
  4618. {
  4619.   begin_insertion (smalllisp);
  4620. }
  4621.  
  4622. /* @cartouche/@end cartouche draws box with rounded corners in
  4623.    TeX output.  Right now, just a NOP insertion. */
  4624. cm_cartouche ()
  4625. {
  4626.   begin_insertion (cartouche);
  4627. }
  4628.  
  4629. cm_format ()
  4630. {
  4631.   begin_insertion (format);
  4632. }
  4633.  
  4634. cm_display ()
  4635. {
  4636.   begin_insertion (display);
  4637. }
  4638.  
  4639. cm_itemize ()
  4640. {
  4641.   begin_insertion (itemize);
  4642. }
  4643.  
  4644. cm_enumerate ()
  4645. {
  4646.   do_enumeration (enumerate, "1");
  4647. }
  4648.  
  4649. /* Start an enumeration insertion of type TYPE.  If the user supplied
  4650.    no argument on the line, then use DEFAULT_STRING as the initial string. */
  4651. do_enumeration (type, default_string)
  4652.      int type;
  4653.      char *default_string;
  4654. {
  4655.   get_until_in_line (".", &enumeration_arg);
  4656.   canon_white (enumeration_arg);
  4657.  
  4658.   if (!*enumeration_arg)
  4659.     {
  4660.       free (enumeration_arg);
  4661.       enumeration_arg = savestring (default_string);
  4662.     }
  4663.  
  4664.   if (!isdigit (*enumeration_arg) && !isletter (*enumeration_arg))
  4665.     {
  4666.       warning ("%s requires a letter or a digit", insertion_type_pname (type));
  4667.  
  4668.       switch (type)
  4669.     {
  4670.     case enumerate:
  4671.       default_string = "1";
  4672.       break;
  4673.     }
  4674.       enumeration_arg = savestring (default_string);
  4675.     }
  4676.   begin_insertion (type);
  4677. }
  4678.  
  4679. cm_table ()
  4680. {
  4681.   begin_insertion (table);
  4682. }
  4683.  
  4684. cm_ftable ()
  4685. {
  4686.   begin_insertion (ftable);
  4687. }
  4688.  
  4689. cm_vtable ()
  4690. {
  4691.   begin_insertion (vtable);
  4692. }
  4693.  
  4694. cm_group ()
  4695. {
  4696.   begin_insertion (group);
  4697. }
  4698.  
  4699. cm_ifinfo ()
  4700. {
  4701.   begin_insertion (ifinfo);
  4702. }
  4703.  
  4704. /* Begin an insertion where the lines are not filled or indented. */
  4705. cm_flushleft ()
  4706. {
  4707.   begin_insertion (flushleft);
  4708. }
  4709.  
  4710. /* Begin an insertion where the lines are not filled, and each line is
  4711.    forced to the right-hand side of the page. */
  4712. cm_flushright ()
  4713. {
  4714.   begin_insertion (flushright);
  4715. }
  4716.  
  4717.  
  4718. /* **************************************************************** */
  4719. /*                                    */
  4720. /*              Conditional Handling                */
  4721. /*                                    */
  4722. /* **************************************************************** */
  4723.  
  4724. /* A structure which contains `defined' variables. */
  4725. typedef struct _defines {
  4726.   struct _defines *next;
  4727.   char *name;
  4728. } DEFINE;
  4729.  
  4730. /* The linked list of `set' defines. */
  4731. DEFINE *defines = (DEFINE *)NULL;
  4732.  
  4733. /* Add NAME to the list of `set' defines. */
  4734. set (name)
  4735.      char *name;
  4736. {
  4737.   DEFINE *temp;
  4738.  
  4739.   for (temp = defines; temp; temp = temp->next)
  4740.     if (strcmp (name, temp->name) == 0)
  4741.       return;
  4742.  
  4743.   temp = (DEFINE *)xmalloc (sizeof (DEFINE));
  4744.   temp->next = defines;
  4745.   temp->name = savestring (name);
  4746.   defines = temp;
  4747. }
  4748.  
  4749. /* Remove NAME from the list of `set' defines. */
  4750. clear (name)
  4751.      char *name;
  4752. {
  4753.   register DEFINE *temp, *last;
  4754.  
  4755.   last = (DEFINE *)NULL;
  4756.   temp = defines;
  4757.  
  4758.   while (temp)
  4759.     {
  4760.       if (strcmp (temp->name, name) == 0)
  4761.     {
  4762.       if (last)
  4763.         last->next = temp->next;
  4764.       else
  4765.         defines = temp->next;
  4766.  
  4767.       free (temp->name);
  4768.       free (temp);
  4769.       break;
  4770.     }
  4771.       last = temp;
  4772.       temp = temp->next;
  4773.     }
  4774. }
  4775.  
  4776. /* Return non-zero if NAME is present in the list of `set' defines. */
  4777. set_p (name)
  4778.      char *name;
  4779. {
  4780.   register DEFINE *temp;
  4781.  
  4782.   for (temp = defines; temp; temp = temp->next)
  4783.     if (strcmp (temp->name, name) == 0)
  4784.       return (1);
  4785.  
  4786.   return (0);
  4787. }
  4788.  
  4789. /* Conditionally parse based on the current command name. */
  4790. command_name_condition ()
  4791. {
  4792.   char discarder[128];
  4793.  
  4794.   sprintf (discarder, "\n@end %s", command);
  4795.   discard_until (discarder);
  4796.   discard_until ("\n");
  4797. }
  4798.  
  4799. #define SET    1
  4800. #define CLEAR    2
  4801. #define IFSET    3
  4802. #define IFCLEAR    4
  4803.  
  4804. /* Create a variable whose name is the rest of the line. */
  4805. cm_set ()
  4806. {
  4807.   handle_variable (SET);
  4808. }
  4809.  
  4810. /* Remove a variable whose name is the rest of the line. */
  4811. cm_clear ()
  4812. {
  4813.   handle_variable (CLEAR);
  4814. }
  4815.  
  4816. cm_ifset ()
  4817. {
  4818.   handle_variable (IFSET);
  4819. }
  4820.  
  4821. cm_ifclear ()
  4822. {
  4823.   handle_variable (IFCLEAR);
  4824. }
  4825.  
  4826. /* Set, clear, or conditionalize based on ACTION. */
  4827. handle_variable (action)
  4828.      int action;
  4829. {
  4830.   char *name;
  4831.  
  4832.   get_rest_of_line (&name);
  4833.   backup_input_pointer ();
  4834.   canon_white (name);
  4835.  
  4836.   if (!*name)
  4837.     line_error ("@%s requires a name", command);
  4838.   else
  4839.     {
  4840.       switch (action)
  4841.     {
  4842.     case SET:
  4843.       set (name);
  4844.       break;
  4845.  
  4846.     case CLEAR:
  4847.       clear (name);
  4848.       break;
  4849.  
  4850.     case IFSET:
  4851.       if (!set_p (name))
  4852.         {
  4853.           discard_until ("\n@end ifset");
  4854.           discard_until ("\n");
  4855.         }
  4856.       else
  4857.         begin_insertion (ifset);
  4858.       break;
  4859.  
  4860.     case IFCLEAR:
  4861.       if (set_p (name))
  4862.         {
  4863.           discard_until ("\n@end ifclear");
  4864.           discard_until ("\n");
  4865.         }
  4866.       else
  4867.         begin_insertion (ifclear);
  4868.       break;
  4869.     }
  4870.     }
  4871.   free (name);
  4872. }
  4873.  
  4874. /* **************************************************************** */
  4875. /*                                    */
  4876. /*            @itemx, @item                    */
  4877. /*                                    */
  4878. /* **************************************************************** */
  4879.  
  4880. /* Non-zero means a string is in execution, as opposed to a file. */
  4881. int executing_string = 0;
  4882.  
  4883. /* Execute the string produced by formatting the ARGs with FORMAT.  This
  4884.    is like submitting a new file with @include. */
  4885. execute_string (format, arg1, arg2, arg3, arg4, arg5)
  4886.      char *format;
  4887. {
  4888.   static char temp_string[4000];
  4889.   sprintf (temp_string, format, arg1, arg2, arg3, arg4, arg5);
  4890.   strcat (temp_string, "@bye\n");
  4891.   pushfile ();
  4892.   input_text_offset = 0;
  4893.   input_text = temp_string;
  4894.   input_filename = savestring (input_filename);
  4895.   size_of_input_text = strlen (temp_string);
  4896.  
  4897.   executing_string++;
  4898.   reader_loop ();
  4899.  
  4900.   popfile ();
  4901.   executing_string--;
  4902.  
  4903.   free_and_clear (&command);
  4904.   command = savestring ("not bye");
  4905. }
  4906.  
  4907. int itemx_flag = 0;
  4908.  
  4909. cm_itemx ()
  4910. {
  4911.   itemx_flag++;
  4912.   cm_item ();
  4913.   itemx_flag--;
  4914. }
  4915.  
  4916. cm_item ()
  4917. {
  4918.   char *rest_of_line, *item_func;
  4919.  
  4920.   /* Can only hack "@item" while inside of an insertion. */
  4921.   if (insertion_level)
  4922.     {
  4923.       INSERTION_ELT *stack = insertion_stack;
  4924.  
  4925.       get_rest_of_line (&rest_of_line);
  4926.       canon_white (rest_of_line);
  4927.       item_func = current_item_function ();
  4928.  
  4929.       /* Okay, do the right thing depending on which insertion function
  4930.      is active. */
  4931.  
  4932.     switch_top:
  4933.       switch (stack->insertion)
  4934.     {
  4935.     case ifinfo:
  4936.     case ifset:
  4937.     case ifclear:
  4938.     case cartouche:
  4939.       stack = stack->next;
  4940.       if (!stack)
  4941.         goto no_insertion;
  4942.       else
  4943.         goto switch_top;
  4944.       break;
  4945.  
  4946.     case menu:
  4947.     case quotation:
  4948.     case example:
  4949.     case smallexample:
  4950.     case lisp:
  4951.     case format:
  4952.     case display:
  4953.     case group:
  4954.       line_error ("The `@%s' command is meaningless within a `@%s' block",
  4955.               command,
  4956.               insertion_type_pname (current_insertion_type ()));
  4957.       break;
  4958.  
  4959.     case itemize:
  4960.     case enumerate:
  4961.       if (itemx_flag)
  4962.         {
  4963.           line_error ("@itemx is not meaningful inside of a `%s' block",
  4964.               insertion_type_pname (current_insertion_type ()));
  4965.         }
  4966.       else
  4967.         {
  4968.           start_paragraph ();
  4969.           kill_self_indent (-1);
  4970.           filling_enabled = indented_fill = 1;
  4971.  
  4972.           if (current_insertion_type () == itemize)
  4973.         {
  4974.           indent (output_column = current_indent - 2);
  4975.  
  4976.           /* I need some way to determine whether this command
  4977.              takes braces or not.  I believe the user can type
  4978.              either "@bullet" or "@bullet{}".  Of course, they
  4979.              can also type "o" or "#" or whatever else they want. */
  4980.           if (item_func && *item_func)
  4981.             {
  4982.               if (*item_func == '@')
  4983.             if (item_func[strlen (item_func) - 1] != '}')
  4984.               execute_string ("%s{}", item_func);
  4985.             else
  4986.               execute_string ("%s", item_func);
  4987.               else
  4988.             execute_string ("%s", item_func);
  4989.             }
  4990.           insert (' ');
  4991.           output_column++;
  4992.         }
  4993.           else
  4994.         enumerate_item ();
  4995.  
  4996.           /* Special hack.  This makes close paragraph ignore you until
  4997.          the start_paragraph () function has been called. */
  4998.           must_start_paragraph = 1;
  4999.         }
  5000.       break;
  5001.  
  5002.     case table:
  5003.     case ftable:
  5004.     case vtable:
  5005.       {
  5006.         /* Get rid of extra characters. */
  5007.         kill_self_indent (-1);
  5008.  
  5009.         /* close_paragraph () almost does what we want.  The problem
  5010.            is when paragraph_is_open, and last_char_was_newline, and
  5011.            the last newline has been turned into a space, because
  5012.            filling_enabled. I handle it here. */
  5013.         if (last_char_was_newline && filling_enabled && paragraph_is_open)
  5014.           insert ('\n');
  5015.         close_paragraph ();
  5016.  
  5017. #if defined (INDENT_PARAGRAPHS_IN_TABLE)
  5018.         /* Indent on a new line, but back up one indentation level. */
  5019.         /* force existing indentation. */
  5020.         {
  5021.           int t;
  5022.  
  5023.           t = inhibit_paragraph_indentation;
  5024.           inhibit_paragraph_indentation = 1;
  5025.           add_char ('i');
  5026.           inhibit_paragraph_indentation = t;
  5027.         }
  5028. #else /* !INDENT_PARAGRAPHS_IN_TABLE */
  5029.         add_char ('i');
  5030. #endif /* !INDENT_PARAGRAPHS_IN_TABLE */
  5031.  
  5032.         output_paragraph_offset--;
  5033.         kill_self_indent (default_indentation_increment + 1);
  5034.  
  5035.         /* Add item's argument to the line. */
  5036.         filling_enabled = 0;
  5037.         if (!item_func && !(*item_func))
  5038.           execute_string ("%s", rest_of_line);
  5039.         else
  5040.           execute_string ("%s{%s}", item_func, rest_of_line);
  5041.  
  5042.         if (current_insertion_type () == ftable)
  5043.           execute_string ("@findex %s\n", rest_of_line);
  5044.  
  5045.         if (current_insertion_type () == vtable)
  5046.           execute_string ("@vindex %s\n", rest_of_line);
  5047.  
  5048.         /* Start a new line, and let start_paragraph ()
  5049.            do the indenting of it for you. */
  5050.         close_single_paragraph ();
  5051.         indented_fill = filling_enabled = 1;
  5052.       }
  5053.     }
  5054.       free (rest_of_line);
  5055.     }
  5056.   else
  5057.     {
  5058.     no_insertion:
  5059.       line_error ("@%s found outside of an insertion block", command);
  5060.     }
  5061. }
  5062.  
  5063.  
  5064. /* **************************************************************** */
  5065. /*                                    */
  5066. /*            Defun and Friends                   */
  5067. /*                                    */
  5068. /* **************************************************************** */
  5069.  
  5070. #define DEFUN_SELF_DELIMITING(c)                    \
  5071.   (((c) == '(')                                \
  5072.    || ((c) == ')')                            \
  5073.    || ((c) == '[')                            \
  5074.    || ((c) == ']'))
  5075.  
  5076. struct token_accumulator
  5077. {
  5078.   unsigned int length;
  5079.   unsigned int index;
  5080.   char **tokens;
  5081. };
  5082.  
  5083. void
  5084. initialize_token_accumulator (accumulator)
  5085.      struct token_accumulator *accumulator;
  5086. {
  5087.   (accumulator->length) = 0;
  5088.   (accumulator->index) = 0;
  5089.   (accumulator->tokens) = NULL;
  5090. }
  5091.  
  5092. void
  5093. accumulate_token (accumulator, token)
  5094.      struct token_accumulator *accumulator;
  5095.      char *token;
  5096. {
  5097.   if ((accumulator->index) >= (accumulator->length))
  5098.     {
  5099.       (accumulator->length) += 10;
  5100.       (accumulator->tokens) = (char **) xrealloc
  5101.     (accumulator->tokens, (accumulator->length * sizeof (char *)));
  5102.     }
  5103.   accumulator->tokens[accumulator->index] = token;
  5104.   accumulator->index += 1;
  5105. }
  5106.  
  5107. char *
  5108. copy_substring (start, end)
  5109.      char *start;
  5110.      char *end;
  5111. {
  5112.   char *result, *scan, *scan_result;
  5113.  
  5114.   result = (char *) xmalloc ((end - start) + 1);
  5115.   scan_result = result;
  5116.   scan = start;
  5117.  
  5118.   while (scan < end)
  5119.     *scan_result++ = *scan++;
  5120.  
  5121.   *scan_result = '\0';
  5122.   return (result);
  5123. }
  5124.  
  5125. /* Given `string' pointing at an open brace, skip forward and return a
  5126.    pointer to just past the matching close brace. */
  5127. int
  5128. scan_group_in_string (string_pointer)
  5129.      char **string_pointer;
  5130. {
  5131.   register int c;
  5132.   register char *scan_string;
  5133.   register unsigned int level = 1;
  5134.  
  5135.   scan_string = (*string_pointer) + 1;
  5136.  
  5137.   while (1)
  5138.     {
  5139.       if (level == 0)
  5140.     {
  5141.       (*string_pointer) = scan_string;
  5142.       return (1);
  5143.     }
  5144.       c = (*scan_string++);
  5145.       if (c == '\0')
  5146.     {
  5147.       /* Tweak line_number to compensate for fact that
  5148.          we gobbled the whole line before coming here. */
  5149.       line_number -= 1;
  5150.       line_error ("Missing `}' in @def arg");
  5151.       line_number += 1;
  5152.       (*string_pointer) = (scan_string - 1);
  5153.       return (0);
  5154.     }
  5155.       if (c == '{')
  5156.     level += 1;
  5157.       if (c == '}')
  5158.     level -= 1;
  5159.     }
  5160. }
  5161.  
  5162. /* Return a list of tokens from the contents of `string'.
  5163.    Commands and brace-delimited groups count as single tokens.
  5164.    Contiguous whitespace characters are converted to a token
  5165.    consisting of a single space. */
  5166. char **
  5167. args_from_string (string)
  5168.      char *string;
  5169. {
  5170.   struct token_accumulator accumulator;
  5171.   register char *scan_string = string;
  5172.   char *token_start, *token_end;
  5173.  
  5174.   initialize_token_accumulator (&accumulator);
  5175.  
  5176.   while ((*scan_string) != '\0')
  5177.     {
  5178.       /* Replace arbitrary whitespace by a single space. */
  5179.       if (whitespace (*scan_string))
  5180.     {
  5181.       scan_string += 1;
  5182.       while (whitespace (*scan_string))
  5183.         scan_string += 1;
  5184.       accumulate_token ((&accumulator), (savestring (" ")));
  5185.       continue;
  5186.     }
  5187.  
  5188.       /* Commands count as single tokens. */
  5189.       if ((*scan_string) == COMMAND_PREFIX)
  5190.     {
  5191.       token_start = scan_string;
  5192.       scan_string += 1;
  5193.       if (self_delimiting (*scan_string))
  5194.         scan_string += 1;
  5195.       else
  5196.         {
  5197.           register int c;
  5198.           while (1)
  5199.         {
  5200.           c = *scan_string++;
  5201.  
  5202.            if ((c == '\0') || (c == '{') || (whitespace (c)))
  5203.             {
  5204.               scan_string -= 1;
  5205.               break;
  5206.             }
  5207.         }
  5208.  
  5209.           if (*scan_string == '{')
  5210.         {
  5211.           char *s = scan_string;
  5212.           (void) scan_group_in_string (&s);
  5213.           scan_string = s;
  5214.         }
  5215.         }
  5216.       token_end = scan_string;
  5217.     }
  5218.  
  5219.       /* Parentheses and brackets are self-delimiting. */
  5220.       else if (DEFUN_SELF_DELIMITING (*scan_string))
  5221.     {
  5222.       token_start = scan_string;
  5223.       scan_string += 1;
  5224.       token_end = scan_string;
  5225.     }
  5226.  
  5227.       /* Open brace introduces a group that is a single token. */
  5228.       else if (*scan_string == '{')
  5229.     {
  5230.       char *s = scan_string;
  5231.       int balanced = scan_group_in_string (&s);
  5232.  
  5233.       token_start = scan_string + 1;
  5234.       scan_string = s;
  5235.       token_end = balanced ? (scan_string - 1) : scan_string;
  5236.     }
  5237.  
  5238.       /* Otherwise a token is delimited by whitespace, parentheses,
  5239.      brackets, or braces.  A token is also ended by a command. */
  5240.       else
  5241.     {
  5242.       token_start = scan_string;
  5243.  
  5244.       while (1)
  5245.         {
  5246.           register int c;
  5247.  
  5248.           c = *scan_string++;
  5249.  
  5250.           if (!c ||
  5251.           (whitespace (c) || DEFUN_SELF_DELIMITING (c) ||
  5252.            c == '{' || c == '}'))
  5253.         {
  5254.           scan_string--;
  5255.           break;
  5256.         }
  5257.  
  5258.           /* If we encounter a command imbedded within a token,
  5259.          then end the token. */
  5260.           if (c == COMMAND_PREFIX)
  5261.         {
  5262.           scan_string--;
  5263.           break;
  5264.         }
  5265.         }
  5266.       token_end = scan_string;
  5267.     }
  5268.  
  5269.       accumulate_token
  5270.     (&accumulator, copy_substring (token_start, token_end));
  5271.     }
  5272.   accumulate_token (&accumulator, NULL);
  5273.   return (accumulator.tokens);
  5274. }
  5275.  
  5276. void
  5277. process_defun_args (defun_args, auto_var_p)
  5278.      char **defun_args;
  5279.      int auto_var_p;
  5280. {
  5281.   int pending_space = 0;
  5282.  
  5283.   while (1)
  5284.     {
  5285.       char *defun_arg = *defun_args++;
  5286.  
  5287.       if (defun_arg == NULL)
  5288.     break;
  5289.  
  5290.       if (defun_arg[0] == ' ')
  5291.     {
  5292.       pending_space = 1;
  5293.       continue;
  5294.     }
  5295.  
  5296.       if (pending_space)
  5297.     {
  5298.       add_char (' ');
  5299.       pending_space = 0;
  5300.     }
  5301.  
  5302.       if (DEFUN_SELF_DELIMITING (defun_arg[0]))
  5303.     add_char (defun_arg[0]);
  5304.       else if (defun_arg[0] == '&')
  5305.     add_word (defun_arg);
  5306.       else if (defun_arg[0] == COMMAND_PREFIX)
  5307.     execute_string ("%s", defun_arg);
  5308.       else if (auto_var_p)
  5309.     execute_string ("@var{%s}", defun_arg);
  5310.       else
  5311.     add_word (defun_arg);
  5312.     }
  5313. }
  5314.  
  5315. char *
  5316. next_nonwhite_defun_arg (arg_pointer)
  5317.      char ***arg_pointer;
  5318. {
  5319.   char **scan = (*arg_pointer);
  5320.   char *arg = (*scan++);
  5321.  
  5322.   if ((arg != 0) && (*arg == ' '))
  5323.     arg = *scan++;
  5324.  
  5325.   if (arg == 0)
  5326.     scan -= 1;
  5327.  
  5328.   *arg_pointer = scan;
  5329.  
  5330.   return ((arg == 0) ? "" : arg);
  5331. }
  5332.  
  5333. /* Make the defun type insertion.
  5334.    TYPE says which insertion this is.
  5335.    X_P says not to start a new insertion if non-zero. */
  5336. void
  5337. defun_internal (type, x_p)
  5338.      enum insertion_type type;
  5339.      int x_p;
  5340. {
  5341.   enum insertion_type base_type;
  5342.   char **defun_args, **scan_args;
  5343.   char *category, *defined_name, *type_name, *type_name2;
  5344.  
  5345.   {
  5346.     char *line;
  5347.     get_rest_of_line (&line);
  5348.     defun_args = (args_from_string (line));
  5349.     free (line);
  5350.   }
  5351.  
  5352.   scan_args = defun_args;
  5353.  
  5354.   switch (type)
  5355.     {
  5356.     case defun:
  5357.       category = "Function";
  5358.       base_type = deffn;
  5359.       break;
  5360.     case defmac:
  5361.       category = "Macro";
  5362.       base_type = deffn;
  5363.       break;
  5364.     case defspec:
  5365.       category = "Special Form";
  5366.       base_type = deffn;
  5367.       break;
  5368.     case defvar:
  5369.       category = "Variable";
  5370.       base_type = defvr;
  5371.       break;
  5372.     case defopt:
  5373.       category = "User Option";
  5374.       base_type = defvr;
  5375.       break;
  5376.     case deftypefun:
  5377.       category = "Function";
  5378.       base_type = deftypefn;
  5379.       break;
  5380.     case deftypevar:
  5381.       category = "Variable";
  5382.       base_type = deftypevr;
  5383.       break;
  5384.     case defivar:
  5385.       category = "Instance Variable";
  5386.       base_type = defcv;
  5387.       break;
  5388.     case defmethod:
  5389.       category = "Method";
  5390.       base_type = defop;
  5391.       break;
  5392.     case deftypemethod:
  5393.       category = "Method";
  5394.       base_type = deftypemethod;
  5395.       break;
  5396.     default:
  5397.       category = next_nonwhite_defun_arg (&scan_args);
  5398.       base_type = type;
  5399.       break;
  5400.     }
  5401.  
  5402.   if ((base_type == deftypefn)
  5403.       || (base_type == deftypevr)
  5404.       || (base_type == defcv)
  5405.       || (base_type == defop)
  5406.       || (base_type == deftypemethod))
  5407.     type_name = next_nonwhite_defun_arg (&scan_args);
  5408.  
  5409.   if (base_type == deftypemethod)
  5410.     type_name2 = next_nonwhite_defun_arg (&scan_args);
  5411.  
  5412.   defined_name = next_nonwhite_defun_arg (&scan_args);
  5413.  
  5414.   /* This hack exists solely for the purposes of formatting the texinfo
  5415.      manual.  I couldn't think of a better way.  The token might be
  5416.      a simple @@ followed immediately by more text.  If this is the case,
  5417.      then the next defun arg is part of this one, and we should concatenate
  5418.      them. */
  5419.   if (*scan_args && **scan_args && !whitespace (**scan_args) &&
  5420.       (strcmp (defined_name, "@@") == 0))
  5421.     {
  5422.       char *tem = (char *)xmalloc (3 + strlen (scan_args[0]));
  5423.  
  5424.       sprintf (tem, "@@%s", scan_args[0]);
  5425.  
  5426.       free (scan_args[0]);
  5427.       scan_args[0] = tem;
  5428.       scan_args++;
  5429.       defined_name = tem;
  5430.     }
  5431.  
  5432.   if (!x_p)
  5433.     begin_insertion (type);
  5434.  
  5435.   /* Write the definition header line.
  5436.      This should start at the normal indentation.  */
  5437.   current_indent -= default_indentation_increment;
  5438.   start_paragraph ();
  5439.  
  5440.   switch (base_type)
  5441.     {
  5442.     case deffn:
  5443.     case defvr:
  5444.     case deftp:
  5445.       execute_string (" -- %s: %s", category, defined_name);
  5446.       break;
  5447.     case deftypefn:
  5448.     case deftypevr:
  5449.       execute_string (" -- %s: %s %s", category, type_name, defined_name);
  5450.       break;
  5451.     case defcv:
  5452.       execute_string (" -- %s of %s: %s", category, type_name, defined_name);
  5453.       break;
  5454.     case defop:
  5455.       execute_string (" -- %s on %s: %s", category, type_name, defined_name);
  5456.       break;
  5457.     case deftypemethod:
  5458.       execute_string (" -- %s on %s: %s %s", category, type_name, type_name2,
  5459.               defined_name);
  5460.       break;
  5461.     }
  5462.   current_indent += default_indentation_increment;
  5463.  
  5464.   /* Now process the function arguments, if any.
  5465.      If these carry onto the next line, they should be indented by two
  5466.      increments to distinguish them from the body of the definition,
  5467.      which is indented by one increment.  */
  5468.   current_indent += default_indentation_increment;
  5469.  
  5470.   switch (base_type)
  5471.     {
  5472.     case deffn:
  5473.     case defop:
  5474.       process_defun_args (scan_args, 1);
  5475.       break;
  5476.     case deftp:
  5477.     case deftypefn:
  5478.     case deftypemethod:
  5479.       process_defun_args (scan_args, 0);
  5480.       break;
  5481.     }
  5482.   current_indent -= default_indentation_increment;
  5483.   close_single_paragraph ();
  5484.  
  5485.   /* Make an entry in the appropriate index. */
  5486.   switch (base_type)
  5487.     {
  5488.     case deffn:
  5489.     case deftypefn:
  5490.       execute_string ("@findex %s\n", defined_name);
  5491.       break;
  5492.     case defvr:
  5493.     case deftypevr:
  5494.     case defcv:
  5495.       execute_string ("@vindex %s\n", defined_name);
  5496.       break;
  5497.     case defop:
  5498.     case deftypemethod:
  5499.       execute_string ("@findex %s on %s\n", defined_name, type_name);
  5500.       break;
  5501.     case deftp:
  5502.       execute_string ("@tindex %s\n", defined_name);
  5503.       break;
  5504.     }
  5505.  
  5506.   /* Deallocate the token list. */
  5507.   scan_args = defun_args;
  5508.   while (1)
  5509.     {
  5510.       char * arg = (*scan_args++);
  5511.       if (arg == NULL)
  5512.     break;
  5513.       free (arg);
  5514.     }
  5515.   free (defun_args);
  5516. }
  5517.  
  5518. /* Add an entry for a function, macro, special form, variable, or option.
  5519.    If the name of the calling command ends in `x', then this is an extra
  5520.    entry included in the body of an insertion of the same type. */
  5521. cm_defun ()
  5522. {
  5523.   int x_p;
  5524.   enum insertion_type type;
  5525.   char *temp = savestring (command);
  5526.  
  5527.   x_p = (command[strlen (command) - 1] == 'x');
  5528.  
  5529.   if (x_p)
  5530.     temp[strlen (temp) - 1] = '\0';
  5531.  
  5532.   type = find_type_from_name (temp);
  5533.   free (temp);
  5534.  
  5535.   /* If we are adding to an already existing insertion, then make sure
  5536.      that we are already in an insertion of type TYPE. */
  5537.   if (x_p &&
  5538.       (!insertion_level || insertion_stack->insertion != type))
  5539.     {
  5540.       line_error ("Must be in a `%s' insertion in order to use `%s'x",
  5541.           command, command);
  5542.       discard_until ("\n");
  5543.       return;
  5544.     }
  5545.  
  5546.   defun_internal (type, x_p);
  5547. }
  5548.  
  5549. /* End existing insertion block. */
  5550. cm_end ()
  5551. {
  5552.   char *temp;
  5553.   enum insertion_type type;
  5554.  
  5555.   if (!insertion_level)
  5556.     {
  5557.       line_error ("Unmatched `@%s'", command);
  5558.       return;
  5559.     }
  5560.  
  5561.   get_rest_of_line (&temp);
  5562.   canon_white (temp);
  5563.  
  5564.   if (strlen (temp) == 0)
  5565.     line_error ("`@%s' needs something after it", command);
  5566.  
  5567.   type = find_type_from_name (temp);
  5568.  
  5569.   if (type == bad_type)
  5570.     {
  5571.       line_error ("Bad argument to `%s', `%s', using `%s'",
  5572.        command, temp, insertion_type_pname (current_insertion_type ()));
  5573.     }
  5574.   end_insertion (type);
  5575.   free (temp);
  5576. }
  5577.  
  5578.  
  5579. /* **************************************************************** */
  5580. /*                                    */
  5581. /*            Other Random Commands                   */
  5582. /*                                    */
  5583. /* **************************************************************** */
  5584.  
  5585. /* This says to inhibit the indentation of the next paragraph, but
  5586.    not of following paragraphs.  */
  5587. cm_noindent ()
  5588. {
  5589.   if (!inhibit_paragraph_indentation)
  5590.     inhibit_paragraph_indentation = -1;
  5591. }
  5592.  
  5593. /* I don't know exactly what to do with this.  Should I allow
  5594.    someone to switch filenames in the middle of output?  Since the
  5595.    file could be partially written, this doesn't seem to make sense.
  5596.    Another option: ignore it, since they don't *really* want to
  5597.    switch files.  Finally, complain, or at least warn. */
  5598. cm_setfilename ()
  5599. {
  5600.   char *filename;
  5601.   get_rest_of_line (&filename);
  5602.   /* warning ("`@%s %s' encountered and ignored", command, filename); */
  5603.   free (filename);
  5604. }
  5605.  
  5606. cm_comment ()
  5607. {
  5608.   discard_until ("\n");
  5609. }
  5610.  
  5611. /* @br can be immediately followed by `{}', so we have to read those here.
  5612.    It should simply close the paragraph. */
  5613. cm_br ()
  5614. {
  5615.   if (looking_at ("{}"))
  5616.     input_text_offset += 2;
  5617.  
  5618.   if (curchar () == '\n')
  5619.     {
  5620.       input_text_offset++;
  5621.       line_number++;
  5622.     }
  5623.  
  5624.   close_paragraph ();
  5625. }
  5626.  
  5627.  /* Insert the number of blank lines passed as argument. */
  5628. cm_sp ()
  5629. {
  5630.   int lines;
  5631.   char *line;
  5632.  
  5633.   get_rest_of_line (&line);
  5634.  
  5635.   sscanf (line, "%d", &lines);
  5636.   while (lines--)
  5637.     add_char ('\n');
  5638.   free (line);
  5639. }
  5640.  
  5641. cm_settitle ()
  5642. {
  5643.   discard_until ("\n");
  5644. }
  5645.  
  5646. cm_need ()
  5647. {
  5648.   discard_until ("\n");
  5649. }
  5650.  
  5651. cm_headings ()
  5652. {
  5653.   discard_until ("\n");
  5654. }
  5655.  
  5656. /* Start a new line with just this text on it.
  5657.    Then center the line of text.
  5658.    This always ends the current paragraph. */
  5659. cm_center ()
  5660. {
  5661.   char *line;
  5662.   int start, length;
  5663.  
  5664.   close_paragraph ();
  5665.   filling_enabled = indented_fill = 0;
  5666.  
  5667.   get_rest_of_line (&line);
  5668.  
  5669.   start = output_paragraph_offset;
  5670.   execute_string (line);
  5671.   length = output_paragraph_offset - start;
  5672.   free (line);
  5673.  
  5674.   if (length < fill_column)
  5675.     {
  5676.       int i;
  5677.  
  5678.       line = (char *)xmalloc (1 + length);
  5679.       strncpy (line, (char *)output_paragraph + start, length);
  5680.       line[length] = '\0';
  5681.  
  5682.       i = (fill_column - length) / 2;
  5683.       output_paragraph_offset = start;
  5684.       while (i--)
  5685.     insert (' ');
  5686.  
  5687.       for (i = 0; i < length; i++)
  5688.     insert (line[i]);
  5689.  
  5690.       free (line);
  5691.     }
  5692.  
  5693.   insert ('\n');
  5694.   close_paragraph ();
  5695.   filling_enabled = 1;
  5696. }
  5697.  
  5698. /* Show what an expression returns. */
  5699. cm_result (arg)
  5700.      int arg;
  5701. {
  5702.   if (arg == END)
  5703.     add_word ("=>");
  5704. }
  5705.  
  5706. /* What an expression expands to. */
  5707. cm_expansion (arg)
  5708.      int arg;
  5709. {
  5710.   if (arg == END)
  5711.     add_word ("==>");
  5712. }
  5713.  
  5714. /* Indicates two expressions are equivalent. */
  5715. cm_equiv (arg)
  5716.      int arg;
  5717. {
  5718.   if (arg == END)
  5719.     add_word ("==");
  5720. }
  5721.  
  5722. /* What an expression may print. */
  5723. cm_print (arg)
  5724.      int arg;
  5725. {
  5726.   if (arg == END)
  5727.     add_word ("-|");
  5728. }
  5729.  
  5730. /* An error signaled. */
  5731. cm_error (arg)
  5732.      int arg;
  5733. {
  5734.   if (arg == END)
  5735.     add_word ("error-->");
  5736. }
  5737.  
  5738. /* The location of point in an example of a buffer. */
  5739. cm_point (arg)
  5740.      int arg;
  5741. {
  5742.   if (arg == END)
  5743.     add_word ("-!-");
  5744. }
  5745.  
  5746. /* Start a new line with just this text on it.
  5747.    The text is outdented one level if possible. */
  5748. cm_exdent ()
  5749. {
  5750.   char *line;
  5751.   int i = current_indent;
  5752.  
  5753.   if (current_indent)
  5754.     current_indent -= default_indentation_increment;
  5755.  
  5756.   get_rest_of_line (&line);
  5757.   close_single_paragraph ();
  5758.   add_word_args ("%s", line);
  5759.   current_indent = i;
  5760.   free (line);
  5761.   close_single_paragraph ();
  5762. }
  5763.  
  5764. cm_include ()
  5765. {
  5766.   cm_infoinclude ();
  5767. }
  5768.  
  5769. /* Remember this file, and move onto the next. */
  5770. cm_infoinclude ()
  5771. {
  5772.   char *filename;
  5773.  
  5774.   close_paragraph ();
  5775.   get_rest_of_line (&filename);
  5776.   pushfile ();
  5777.  
  5778.   /* In verbose mode we print info about including another file. */
  5779.   if (verbose_mode)
  5780.     {
  5781.       register int i = 0;
  5782.       register FSTACK *stack = filestack;
  5783.  
  5784.       for (i = 0, stack = filestack; stack; stack = stack->next, i++);
  5785.  
  5786.       i *= 2;
  5787.  
  5788.       printf ("%*s", i, "");
  5789.       printf ("%c%s %s\n", COMMAND_PREFIX, command, filename);
  5790.       fflush (stdout);
  5791.     }
  5792.  
  5793.   if (!find_and_load (filename))
  5794.     {
  5795.       extern char *sys_errlist[];
  5796.       extern int errno, sys_nerr;
  5797.       popfile ();
  5798.  
  5799.       /* Cannot "@include foo", in line 5 of "/wh/bar". */
  5800.       line_error ("`%c%s %s': %s", COMMAND_PREFIX, command, filename,
  5801.           ((errno < sys_nerr) ?
  5802.            sys_errlist[errno] : "Unknown file system error"));
  5803.     }
  5804.   free (filename);
  5805. }
  5806.  
  5807. /* The other side of a malformed expression. */
  5808. misplaced_brace ()
  5809. {
  5810.   line_error ("Misplaced `}'");
  5811. }
  5812.  
  5813. /* Don't let the filling algorithm insert extra whitespace here. */
  5814. cm_force_abbreviated_whitespace ()
  5815. {
  5816. }
  5817.  
  5818. /* Do not let this character signify the end of a sentence, though
  5819.    if it was seen without the command prefix it normally would.  We
  5820.    do this by turning on the 8th bit of the character. */
  5821. cm_ignore_sentence_ender ()
  5822. {
  5823.   add_char (META ((*command)));
  5824. }
  5825.  
  5826. /* Signals end of processing.  Easy to make this happen. */
  5827. cm_bye ()
  5828. {
  5829.   input_text_offset = size_of_input_text;
  5830. }
  5831.  
  5832. cm_asis ()
  5833. {
  5834. }
  5835.  
  5836. cm_math ()
  5837. {
  5838. }
  5839.  
  5840. cm_setchapternewpage ()
  5841. {
  5842.   discard_until ("\n");
  5843. }
  5844.  
  5845. cm_smallbook ()
  5846. {
  5847.   discard_until ("\n");
  5848. }
  5849.  
  5850. /* **************************************************************** */
  5851. /*                                    */
  5852. /*            Indexing Stuff                    */
  5853. /*                                    */
  5854. /* **************************************************************** */
  5855.  
  5856.  
  5857. /* An index element... */
  5858. typedef struct index_elt
  5859. {
  5860.   struct index_elt *next;
  5861.   char *entry;            /* The index entry itself. */
  5862.   char *node;            /* The node from whence it came. */
  5863.   int code;            /* Non-zero means add `@code{...}' when
  5864.                    printing this element. */
  5865.   int defining_line;        /* Line number where this entry was written. */
  5866. } INDEX_ELT;
  5867.  
  5868. /* A list of short-names for each index, and the index to that index in our
  5869.    index array, the_indices.  In addition, for each index, it is remembered
  5870.    whether that index is a code index or not.  Code indices have @code{}
  5871.    inserted around the first word when they are printed with printindex. */
  5872. typedef struct
  5873. {
  5874.   char *name;
  5875.   int index;
  5876.   int code;
  5877. } INDEX_ALIST;
  5878.  
  5879. INDEX_ALIST **name_index_alist = (INDEX_ALIST **) NULL;
  5880.  
  5881. /* An array of pointers.  Each one is for a different index.  The
  5882.    "synindex" command changes which array slot is pointed to by a
  5883.    given "index". */
  5884. INDEX_ELT **the_indices = (INDEX_ELT **) NULL;
  5885.  
  5886. /* The number of defined indices. */
  5887. int defined_indices = 0;
  5888.  
  5889. /* We predefine these. */
  5890. #define program_index 0
  5891. #define function_index 1
  5892. #define concept_index 2
  5893. #define variable_index 3
  5894. #define datatype_index 4
  5895. #define key_index 5
  5896.  
  5897. init_indices ()
  5898. {
  5899.   int i;
  5900.  
  5901.   /* Create the default data structures. */
  5902.  
  5903.   /* Initialize data space. */
  5904.   if (!the_indices)
  5905.     {
  5906.       the_indices = (INDEX_ELT **) xmalloc ((1 + defined_indices) *
  5907.                         sizeof (INDEX_ELT *));
  5908.       the_indices[defined_indices] = (INDEX_ELT *) NULL;
  5909.  
  5910.       name_index_alist = (INDEX_ALIST **) xmalloc ((1 + defined_indices) *
  5911.                            sizeof (INDEX_ALIST *));
  5912.       name_index_alist[defined_indices] = (INDEX_ALIST *) NULL;
  5913.     }
  5914.  
  5915.   /* If there were existing indices, get rid of them now. */
  5916.   for (i = 0; i < defined_indices; i++)
  5917.     undefindex (name_index_alist[i]->name);
  5918.  
  5919.   /* Add the default indices. */
  5920.   defindex ("pg", 0);
  5921.   defindex ("fn", 1);        /* "fn" is a code index.  */
  5922.   defindex ("cp", 0);
  5923.   defindex ("vr", 0);
  5924.   defindex ("tp", 0);
  5925.   defindex ("ky", 0);
  5926.  
  5927. }
  5928.  
  5929. /* Find which element in the known list of indices has this name.
  5930.    Returns -1 if NAME isn't found. */
  5931. int
  5932. find_index_offset (name)
  5933.      char *name;
  5934. {
  5935.   register int i;
  5936.   for (i = 0; i < defined_indices; i++)
  5937.     if (name_index_alist[i] &&
  5938.     stricmp (name, name_index_alist[i]->name) == 0)
  5939.       return (name_index_alist[i]->index);
  5940.   return (-1);
  5941. }
  5942.  
  5943. /* Return a pointer to the entry of (name . index) for this name.
  5944.    Return NULL if the index doesn't exist. */
  5945. INDEX_ALIST *
  5946. find_index (name)
  5947.      char *name;
  5948. {
  5949.   int offset = find_index_offset (name);
  5950.   if (offset > -1)
  5951.     return (name_index_alist[offset]);
  5952.   else
  5953.     return ((INDEX_ALIST *) NULL);
  5954. }
  5955.  
  5956. /* Given an index name, return the offset in the_indices of this index,
  5957.    or -1 if there is no such index. */
  5958. translate_index (name)
  5959.      char *name;
  5960. {
  5961.   INDEX_ALIST *which = find_index (name);
  5962.  
  5963.   if (which)
  5964.     return (which->index);
  5965.   else
  5966.     return (-1);
  5967. }
  5968.  
  5969. /* Return the index list which belongs to NAME. */
  5970. INDEX_ELT *
  5971. index_list (name)
  5972.      char *name;
  5973. {
  5974.   int which = translate_index (name);
  5975.   if (which < 0)
  5976.     return ((INDEX_ELT *) - 1);
  5977.   else
  5978.     return (the_indices[which]);
  5979. }
  5980.  
  5981. /* Please release me, let me go... */
  5982. free_index (index)
  5983.      INDEX_ELT *index;
  5984. {
  5985.   INDEX_ELT *temp;
  5986.  
  5987.   while ((temp = index) != (INDEX_ELT *) NULL)
  5988.     {
  5989.       free (temp->entry);
  5990.       free (temp->node);
  5991.       index = index->next;
  5992.       free (temp);
  5993.     }
  5994. }
  5995.  
  5996. /* Flush an index by name. */
  5997. undefindex (name)
  5998.      char *name;
  5999. {
  6000.   int i;
  6001.   int which = find_index_offset (name);
  6002.  
  6003.   if (which < 0)
  6004.     return (which);
  6005.  
  6006.   i = name_index_alist[which]->index;
  6007.  
  6008.  
  6009.   free_index (the_indices[i]);
  6010.   the_indices[i] = (INDEX_ELT *) NULL;
  6011.  
  6012.   free (name_index_alist[which]->name);
  6013.   free (name_index_alist[which]);
  6014.   name_index_alist[which] = (INDEX_ALIST *) NULL;
  6015. }
  6016.  
  6017. /* Define an index known as NAME.  We assign the slot number.
  6018.    CODE if non-zero says to make this a code index. */
  6019. defindex (name, code)
  6020.      char *name;
  6021.      int code;
  6022. {
  6023.   register int i, slot;
  6024.  
  6025.   /* If it already exists, flush it. */
  6026.   undefindex (name);
  6027.  
  6028.   /* Try to find an empty slot. */
  6029.   slot = -1;
  6030.   for (i = 0; i < defined_indices; i++)
  6031.     if (!name_index_alist[i])
  6032.       {
  6033.     slot = i;
  6034.     break;
  6035.       }
  6036.  
  6037.   if (slot < 0)
  6038.     {
  6039.       /* No such luck.  Make space for another index. */
  6040.       slot = defined_indices;
  6041.       defined_indices++;
  6042.  
  6043.       name_index_alist = (INDEX_ALIST **)
  6044.     xrealloc ((char *)name_index_alist,
  6045.           (1 + defined_indices) * sizeof (INDEX_ALIST *));
  6046.       the_indices = (INDEX_ELT **)
  6047.     xrealloc ((char *)the_indices,
  6048.           (1 + defined_indices) * sizeof (INDEX_ELT *));
  6049.     }
  6050.  
  6051.   /* We have a slot.  Start assigning. */
  6052.   name_index_alist[slot] = (INDEX_ALIST *) xmalloc (sizeof (INDEX_ALIST));
  6053.   name_index_alist[slot]->name = savestring (name);
  6054.   name_index_alist[slot]->index = slot;
  6055.   name_index_alist[slot]->code = code;
  6056.  
  6057.   the_indices[slot] = (INDEX_ELT *) NULL;
  6058. }
  6059.  
  6060. /* Add the arguments to the current index command to the index NAME. */
  6061. index_add_arg (name)
  6062.      char *name;
  6063. {
  6064.   int which;
  6065.   char *index_entry;
  6066.   INDEX_ALIST *tem;
  6067.  
  6068.   tem = find_index (name);
  6069.  
  6070.   which = tem ? tem->index : -1;
  6071.  
  6072.   get_rest_of_line (&index_entry);
  6073.   ignore_blank_line ();
  6074.  
  6075.   if (which < 0)
  6076.     {
  6077.       line_error ("Unknown index reference `%s'", name);
  6078.       free (index_entry);
  6079.     }
  6080.   else
  6081.     {
  6082.       INDEX_ELT *new = (INDEX_ELT *) xmalloc (sizeof (INDEX_ELT));
  6083.       new->next = the_indices[which];
  6084.       new->entry = index_entry;
  6085.       new->node = current_node;
  6086.       new->code = tem->code;
  6087.       new->defining_line = line_number - 1;
  6088.       the_indices[which] = new;
  6089.     }
  6090. }
  6091.  
  6092. #define INDEX_COMMAND_SUFFIX "index"
  6093.  
  6094. /* The function which user defined index commands call. */
  6095. gen_index ()
  6096. {
  6097.   char *name = savestring (command);
  6098.   if (strlen (name) >= strlen ("index"))
  6099.     name[strlen (name) - strlen ("index")] = '\0';
  6100.   index_add_arg (name);
  6101.   free (name);
  6102. }
  6103.  
  6104. /* Define a new index command.  Arg is name of index. */
  6105. cm_defindex ()
  6106. {
  6107.   gen_defindex (0);
  6108. }
  6109.  
  6110. cm_defcodeindex ()
  6111. {
  6112.   gen_defindex (1);
  6113. }
  6114.  
  6115. gen_defindex (code)
  6116.      int code;
  6117. {
  6118.   char *name;
  6119.   get_rest_of_line (&name);
  6120.  
  6121.   if (find_index (name))
  6122.     {
  6123.       line_error ("Index `%s' already exists", name);
  6124.       free (name);
  6125.       return;
  6126.     }
  6127.   else
  6128.     {
  6129.       char *temp = (char *) alloca (1 + strlen (name) + strlen ("index"));
  6130.       sprintf (temp, "%sindex", name);
  6131.       define_user_command (temp, gen_index, 0);
  6132.       defindex (name, code);
  6133.       free (name);
  6134.     }
  6135. }
  6136.  
  6137. /* Append LIST2 to LIST1.  Return the head of the list. */
  6138. INDEX_ELT *
  6139. index_append (head, tail)
  6140.      INDEX_ELT *head, *tail;
  6141. {
  6142.   register INDEX_ELT *t_head = head;
  6143.  
  6144.   if (!t_head)
  6145.     return (tail);
  6146.  
  6147.   while (t_head->next)
  6148.     t_head = t_head->next;
  6149.   t_head->next = tail;
  6150.   return (head);
  6151. }
  6152.  
  6153. /* Expects 2 args, on the same line.  Both are index abbreviations.
  6154.    Make the first one be a synonym for the second one, i.e. make the
  6155.    first one have the same index as the second one. */
  6156. cm_synindex ()
  6157. {
  6158.   int redirector, redirectee;
  6159.   char *temp;
  6160.  
  6161.   skip_whitespace ();
  6162.   get_until_in_line (" ", &temp);
  6163.   redirectee = find_index_offset (temp);
  6164.   skip_whitespace ();
  6165.   free_and_clear (&temp);
  6166.   get_until_in_line (" ", &temp);
  6167.   redirector = find_index_offset (temp);
  6168.   free (temp);
  6169.   if (redirector < 0 || redirectee < 0)
  6170.     {
  6171.       line_error ("Unknown index reference");
  6172.     }
  6173.   else
  6174.     {
  6175.       /* I think that we should let the user make indices synonymous to
  6176.          each other without any lossage of info.  This means that one can
  6177.          say @synindex cp dt anywhere in the file, and things that used to
  6178.          be in cp will go into dt. */
  6179.       INDEX_ELT *i1 = the_indices[redirectee], *i2 = the_indices[redirector];
  6180.  
  6181.       if (i1 || i2)
  6182.     {
  6183.       if (i1)
  6184.         the_indices[redirectee] = index_append (i1, i2);
  6185.       else
  6186.         the_indices[redirectee] = index_append (i2, i1);
  6187.     }
  6188.  
  6189.       name_index_alist[redirectee]->index =
  6190.     name_index_alist[redirector]->index;
  6191.     }
  6192. }
  6193.  
  6194. cm_pindex ()            /* Pinhead index. */
  6195. {
  6196.   index_add_arg ("pg");
  6197. }
  6198.  
  6199. cm_vindex ()            /* Variable index. */
  6200. {
  6201.   index_add_arg ("vr");
  6202. }
  6203.  
  6204. cm_kindex ()            /* Key index. */
  6205. {
  6206.   index_add_arg ("ky");
  6207. }
  6208.  
  6209. cm_cindex ()            /* Concept index. */
  6210. {
  6211.   index_add_arg ("cp");
  6212. }
  6213.  
  6214. cm_findex ()            /* Function index. */
  6215. {
  6216.   index_add_arg ("fn");
  6217. }
  6218.  
  6219. cm_tindex ()            /* Data Type index. */
  6220. {
  6221.   index_add_arg ("tp");
  6222. }
  6223.  
  6224. /* Sorting the index. */
  6225. index_element_compare (element1, element2)
  6226.      INDEX_ELT **element1, **element2;
  6227. {
  6228.   /* This needs to ignore leading non-text characters. */
  6229.   return (strcmp ((*element1)->entry, (*element2)->entry));
  6230. }
  6231.  
  6232. /* Sort the index passed in INDEX, returning an array of
  6233.    pointers to elements.  The array is terminated with a NULL
  6234.    pointer.  We call qsort because it's supposed to be fast.
  6235.    I think this looks bad. */
  6236. INDEX_ELT **
  6237. sort_index (index)
  6238.      INDEX_ELT *index;
  6239. {
  6240.   INDEX_ELT *temp = index;
  6241.   INDEX_ELT **array;
  6242.   int count = 0;
  6243.  
  6244.   while (temp != (INDEX_ELT *) NULL)
  6245.     {
  6246.       count++;
  6247.       temp = temp->next;
  6248.     }
  6249.  
  6250.   /* We have the length.  Make an array. */
  6251.  
  6252.   array = (INDEX_ELT **) xmalloc ((count + 1) * sizeof (INDEX_ELT *));
  6253.   count = 0;
  6254.   temp = index;
  6255.  
  6256.   while (temp != (INDEX_ELT *) NULL)
  6257.     {
  6258.       array[count++] = temp;
  6259.       temp = temp->next;
  6260.     }
  6261.   array[count] = (INDEX_ELT *) NULL;    /* terminate the array. */
  6262.  
  6263.   /* Sort the array. */
  6264.   qsort (array, count, sizeof (INDEX_ELT *), index_element_compare);
  6265.  
  6266.   return (array);
  6267. }
  6268.  
  6269. /* Non-zero means that we are in the middle of printing an index. */
  6270. int printing_index = 0;
  6271.  
  6272. /* Takes one arg, a short name of an index to print.
  6273.    Outputs a menu of the sorted elements of the index. */
  6274. cm_printindex ()
  6275. {
  6276.   int item;
  6277.   INDEX_ELT *index;
  6278.   INDEX_ELT **array;
  6279.   char *index_name;
  6280.   int old_inhibitions = inhibit_paragraph_indentation;
  6281.   int previous_filling_enabled_value = filling_enabled;
  6282.  
  6283.   close_paragraph ();
  6284.   get_rest_of_line (&index_name);
  6285.  
  6286.   index = index_list (index_name);
  6287.   if ((int) index == -1)
  6288.     {
  6289.       line_error ("Unknown index name `%s'", index_name);
  6290.       free (index_name);
  6291.       return;
  6292.     }
  6293.   else
  6294.     free (index_name);
  6295.  
  6296.   array = sort_index (index);
  6297.  
  6298.   filling_enabled = 0;
  6299.   inhibit_paragraph_indentation = 1;
  6300.   close_paragraph ();
  6301.   add_word ("* Menu:\n\n");
  6302.  
  6303.   printing_index = 1;
  6304.   for (item = 0; (index = array[item]); item++)
  6305.     {
  6306.       int real_line_number = line_number;
  6307.  
  6308.       /* Let errors generated while making the index entry point back
  6309.      at the line which contains the entry. */
  6310.       line_number = index->defining_line;
  6311.  
  6312.       /* If this particular entry should be printed as a "code" index,
  6313.      then wrap the entry with "@code{...}". */
  6314.       if (index->code)
  6315.     execute_string ("* @code{%s}: ", index->entry);
  6316.       else
  6317.     execute_string ("* %s: ", index->entry);
  6318.  
  6319.       /* Pad the front of the destination nodename so that
  6320.      the output looks nice. */
  6321.       if (fill_column > 40 && output_column < 40)
  6322.     indent (40 - output_column);
  6323.  
  6324.       execute_string ("%s.\n", index->node);
  6325.  
  6326.       line_number = real_line_number;
  6327.       flush_output ();
  6328.     }
  6329.  
  6330.   printing_index = 0;
  6331.   free (array);
  6332.   close_single_paragraph ();
  6333.   filling_enabled = previous_filling_enabled_value;
  6334.   inhibit_paragraph_indentation = old_inhibitions;
  6335. }
  6336.  
  6337.  
  6338. /* **************************************************************** */
  6339. /*                                    */
  6340. /*            Making User Defined Commands            */
  6341. /*                                    */
  6342. /* **************************************************************** */
  6343.  
  6344. define_user_command (name, proc, needs_braces_p)
  6345.      char *name;
  6346.      FUNCTION *proc;
  6347.      int needs_braces_p;
  6348. {
  6349.   int slot = user_command_array_len;
  6350.   user_command_array_len++;
  6351.  
  6352.   if (!user_command_array)
  6353.     user_command_array = (COMMAND **) xmalloc (1 * sizeof (COMMAND *));
  6354.  
  6355.   user_command_array = (COMMAND **) xrealloc (user_command_array,
  6356.                           (1 + user_command_array_len) *
  6357.                           sizeof (COMMAND *));
  6358.  
  6359.   user_command_array[slot] = (COMMAND *) xmalloc (sizeof (COMMAND));
  6360.   user_command_array[slot]->name = savestring (name);
  6361.   user_command_array[slot]->proc = proc;
  6362.   user_command_array[slot]->argument_in_braces = needs_braces_p;
  6363. }
  6364.  
  6365. /* Make ALIAS run the named FUNCTION.  Copies properties from FUNCTION. */
  6366. define_alias (alias, function)
  6367.      char *alias, *function;
  6368. {
  6369. }
  6370.  
  6371. /* Set the paragraph indentation variable to the value specified in STRING.
  6372.    Values can be:
  6373.    `asis': Don't change existing indentation.
  6374.    `none': Remove existing indentation.
  6375.       NUM: Indent NUM spaces at the starts of paragraphs.
  6376.            Note that if NUM is zero, we assume `none'.
  6377.  
  6378.    Returns 0 if successful, or non-zero if STRING isn't one of the above. */
  6379. int
  6380. set_paragraph_indent (string)
  6381.      char *string;
  6382. {
  6383.   if (strcmp (string, "asis") == 0)
  6384.     paragraph_start_indent = 0;
  6385.   else if (strcmp (string, "none") == 0)
  6386.     paragraph_start_indent = -1;
  6387.   else
  6388.     {
  6389.       if (sscanf (string, "%d", ¶graph_start_indent) != 1)
  6390.     return (-1);
  6391.       else
  6392.     {
  6393.       if (paragraph_start_indent == 0)
  6394.         paragraph_start_indent = -1;
  6395.     }
  6396.     }
  6397.   return (0);
  6398. }
  6399.  
  6400. cm_paragraphindent ()
  6401. {
  6402.   char *arg;
  6403.  
  6404.   get_rest_of_line (&arg);
  6405.   if (set_paragraph_indent (arg) != 0)
  6406.     line_error ("Bad argument to @paragraphindent");
  6407.  
  6408.   free (arg);
  6409. }
  6410.  
  6411. /* Some support for footnotes. */
  6412.  
  6413. /* Footnotes are a new construct in Info.  We don't know the best method
  6414.    of implementing them for sure, so we present two possiblities.
  6415.  
  6416.    SeparateNode:
  6417.     Make them look like followed references, with the reference
  6418.     destinations in a makeinfo manufactured node or,
  6419.  
  6420.    EndNode:
  6421.     Make them appear at the bottom of the node that they originally
  6422.     appeared in. */
  6423. #define SeparateNode 0
  6424. #define EndNode 1
  6425.  
  6426. int footnote_style = EndNode;
  6427. int first_footnote_this_node = 1;
  6428. int footnote_count = 0;
  6429.  
  6430. /* Set the footnote style based on he style identifier in STRING. */
  6431. int
  6432. set_footnote_style (string)
  6433.      char *string;
  6434. {
  6435.   if ((stricmp (string, "separate") == 0) ||
  6436.       (stricmp (string, "MN") == 0))
  6437.     footnote_style = SeparateNode;
  6438.   else if ((stricmp (string, "end") == 0) ||
  6439.        (stricmp (string, "EN") == 0))
  6440.     footnote_style = EndNode;
  6441.   else
  6442.     return (-1);
  6443.  
  6444.  return (0);
  6445. }
  6446.  
  6447. cm_footnotestyle ()
  6448. {
  6449.   char *arg;
  6450.  
  6451.   get_rest_of_line (&arg);
  6452.  
  6453.   if (set_footnote_style (arg) != 0)
  6454.     line_error ("Bad argument to @footnotestyle");
  6455.  
  6456.   free (arg);
  6457. }
  6458.  
  6459. typedef struct fn
  6460. {
  6461.   struct fn *next;
  6462.   char *marker;
  6463.   char *note;
  6464. }  FN;
  6465.  
  6466. FN *pending_notes = (FN *) NULL;
  6467.  
  6468. /* A method for remembering footnotes.  Note that this list gets output
  6469.    at the end of the current node. */
  6470. remember_note (marker, note)
  6471.      char *marker, *note;
  6472. {
  6473.   FN *temp = (FN *) xmalloc (sizeof (FN));
  6474.  
  6475.   temp->marker = savestring (marker);
  6476.   temp->note = savestring (note);
  6477.   temp->next = pending_notes;
  6478.   pending_notes = temp;
  6479.   footnote_count++;
  6480. }
  6481.  
  6482. /* How to get rid of existing footnotes. */
  6483. free_pending_notes ()
  6484. {
  6485.   FN *temp;
  6486.  
  6487.   while ((temp = pending_notes) != (FN *) NULL)
  6488.     {
  6489.       free (temp->marker);
  6490.       free (temp->note);
  6491.       pending_notes = pending_notes->next;
  6492.       free (temp);
  6493.     }
  6494.   first_footnote_this_node = 1;
  6495.   footnote_count = 0;
  6496. }
  6497.  
  6498. /* What to do when you see a @footnote construct. */
  6499.  
  6500.  /* Handle a "footnote".
  6501.     footnote *{this is a footnote}
  6502.     where "*" is the marker character for this note. */
  6503. cm_footnote ()
  6504. {
  6505.   char *marker;
  6506.   char *note;
  6507.  
  6508.   get_until ("{", &marker);
  6509.   canon_white (marker);
  6510.  
  6511.   /* Read the argument in braces. */
  6512.   if (curchar () != '{')
  6513.     {
  6514.       line_error ("`@%s' expected more than just `%s'.  It needs something in `{...}'", command, marker);
  6515.       free (marker);
  6516.       return;
  6517.     }
  6518.   else
  6519.     {
  6520.       int braces = 1;
  6521.       int temp = ++input_text_offset;
  6522.       int len;
  6523.  
  6524.       while (braces)
  6525.     {
  6526.       if (temp == size_of_input_text)
  6527.         {
  6528.           line_error ("No closing brace for footnote `%s'", marker);
  6529.           return;
  6530.         }
  6531.  
  6532.       if (input_text[temp] == '{')
  6533.         braces++;
  6534.       else if (input_text[temp] == '}')
  6535.         braces--;
  6536.       else if (input_text[temp] == '\n')
  6537.         line_number ++;
  6538.  
  6539.       temp++;
  6540.     }
  6541.  
  6542.       len = (temp - input_text_offset) - 1;
  6543.       note = (char *)xmalloc (len + 1);
  6544.       strncpy (note, &input_text[input_text_offset], len);
  6545.       note[len] = '\0';
  6546.       input_text_offset = temp;
  6547.     }
  6548.  
  6549.   if (!current_node || !*current_node)
  6550.     {
  6551.       line_error ("Footnote defined without parent node");
  6552.       free (marker);
  6553.       free (note);
  6554.       return;
  6555.     }
  6556.  
  6557.   if (!*marker)
  6558.     {
  6559.       free (marker);
  6560.  
  6561.       if (number_footnotes)
  6562.     {
  6563.       marker = (char *)xmalloc (10);
  6564.       sprintf (marker, "%d", current_footnote_number);
  6565.       current_footnote_number++;
  6566.     }
  6567.       else
  6568.     marker = savestring ("*");
  6569.     }
  6570.  
  6571.   remember_note (marker, note);
  6572.  
  6573.   /* Your method should at least insert MARKER. */
  6574.   switch (footnote_style)
  6575.     {
  6576.     case SeparateNode:
  6577.       add_word_args ("(%s)", marker);
  6578.       if (first_footnote_this_node)
  6579.     {
  6580.       char *temp_string;
  6581.  
  6582.       temp_string = (char *)
  6583.         xmalloc ((strlen (current_node)) + (strlen ("-Footnotes")) + 1);
  6584.  
  6585.       add_word_args (" (*note %s-Footnotes::)", current_node);
  6586.       strcpy (temp_string, current_node);
  6587.       strcat (temp_string, "-Footnotes");
  6588.       remember_node_reference (temp_string, line_number, followed_reference);
  6589.       free (temp_string);
  6590.       first_footnote_this_node = 0;
  6591.     }
  6592.       break;
  6593.  
  6594.     case EndNode:
  6595.       add_word_args ("(%s)", marker);
  6596.       break;
  6597.  
  6598.     default:
  6599.       break;
  6600.     }
  6601.   free (marker);
  6602.   free (note);
  6603. }
  6604.  
  6605. /* Non-zero means that we are currently in the process of outputting
  6606.    footnotes. */
  6607. int already_outputting_pending_notes = 0;
  6608.  
  6609. /* Output the footnotes.  We are at the end of the current node. */
  6610. output_pending_notes ()
  6611. {
  6612.   FN *footnote = pending_notes;
  6613.  
  6614.   if (!pending_notes)
  6615.     return;
  6616.  
  6617.   switch (footnote_style)
  6618.     {
  6619.  
  6620.     case SeparateNode:
  6621.       {
  6622.     char *old_current_node = current_node;
  6623.     char *old_command = savestring (command);
  6624.  
  6625.     already_outputting_pending_notes++;
  6626.     execute_string ("@node %s-Footnotes,,,%s\n", current_node, current_node);
  6627.     already_outputting_pending_notes--;
  6628.     current_node = old_current_node;
  6629.     free (command);
  6630.     command = old_command;
  6631.       }
  6632.       break;
  6633.  
  6634.     case EndNode:
  6635.       close_paragraph ();
  6636.       in_fixed_width_font++;
  6637.       execute_string ("---------- Footnotes ----------\n\n");
  6638.       in_fixed_width_font--;
  6639.       break;
  6640.     }
  6641.  
  6642.   /* Handle the footnotes in reverse order. */
  6643.   {
  6644.     FN **array = (FN **) xmalloc ((footnote_count + 1) * sizeof (FN *));
  6645.  
  6646.     array[footnote_count] = (FN *) NULL;
  6647.  
  6648.     while (--footnote_count > -1)
  6649.       {
  6650.     array[footnote_count] = footnote;
  6651.     footnote = footnote->next;
  6652.       }
  6653.  
  6654.     filling_enabled = 1;
  6655.     indented_fill = 1;
  6656.  
  6657.     while (footnote = array[++footnote_count])
  6658.       {
  6659.  
  6660.     switch (footnote_style)
  6661.       {
  6662.       case SeparateNode:
  6663.       case EndNode:
  6664.         execute_string ("(%s)  %s", footnote->marker, footnote->note);
  6665.         close_paragraph ();
  6666.         break;
  6667.       }
  6668.       }
  6669.     close_paragraph ();
  6670.     free (array);
  6671.   }
  6672. }
  6673.  
  6674.  
  6675. /* **************************************************************** */
  6676. /*                                                                  */
  6677. /*              User definable Macros (text substitution)        */
  6678. /*                                                                  */
  6679. /* **************************************************************** */
  6680.  
  6681. #if defined (HAVE_MACROS)
  6682.  
  6683. /* Array of macros and definitions. */
  6684. MACRO_DEF **macro_list = (MACRO_DEF **)NULL;
  6685.  
  6686. int macro_list_len = 0;        /* Number of elements. */
  6687. int macro_list_size = 0;    /* Number of slots in total. */
  6688.  
  6689. /* Return the macro definition of NAME or NULL if NAME is not defined. */
  6690. MACRO_DEF *
  6691. find_macro (name)
  6692.      char *name;
  6693. {
  6694.   register int i;
  6695.   register MACRO_DEF *def;
  6696.  
  6697.   def = (MACRO_DEF *)NULL;
  6698.   for (i = 0; macro_list && (def = macro_list[i]); i++)
  6699.     if (strcmp (def->name, name) == 0)
  6700.       break;
  6701.  
  6702.   return (def);
  6703. }
  6704.  
  6705. /* Add the macro NAME with DEFINITION to macro_list.  FILENAME is
  6706.    the name of the file where this definition can be found, and
  6707.    LINENO is the line number within that file.  If a macro already
  6708.    exists with NAME, then a warning is produced, and that previous
  6709.    definition is overwritten. */
  6710. void
  6711. add_macro (name, definition, filename, lineno)
  6712.      char *name, *definition;
  6713.      char *filename;
  6714.      int lineno;
  6715. {
  6716.   register MACRO_DEF *def;
  6717.  
  6718.   def = find_macro (name);
  6719.  
  6720.   if (!def)
  6721.     {
  6722.       if (macro_list_len + 2 >= macro_list_size)
  6723.     macro_list = (MACRO_DEF **)xrealloc
  6724.       (macro_list, ((macro_list_size += 10) * sizeof (MACRO_DEF *)));
  6725.  
  6726.       macro_list[macro_list_len] = (MACRO_DEF *)xmalloc (sizeof (MACRO_DEF));
  6727.       macro_list[macro_list_len + 1] = (MACRO_DEF *)NULL;
  6728.  
  6729.       def = macro_list[macro_list_len];
  6730.       macro_list_len += 1;
  6731.       def->name = savestring (name);
  6732.     }
  6733.   else
  6734.     {
  6735.       char *temp_filename = input_filename;
  6736.       int temp_line = line_number;
  6737.  
  6738.       warning ("The macro `%s' is previously defined.", name);
  6739.  
  6740.       input_filename = def->filename;
  6741.       line_number = def->lineno;
  6742.  
  6743.       warning ("Here is the previous definition of `%s'.", name);
  6744.  
  6745.       input_filename = temp_filename;
  6746.       line_number = temp_line;
  6747.  
  6748.       free (def->filename);
  6749.       free (def->definition);
  6750.     }
  6751.  
  6752.   def->filename = savestring (filename);
  6753.   def->lineno = lineno;
  6754.   def->definition = savestring (definition);
  6755. }
  6756.  
  6757.  
  6758. /* Delete the macro with name NAME.  The macro is deleted from the list,
  6759.    but it is also returned.  If there was no macro defined, NULL is
  6760.    returned. */
  6761. MACRO_DEF *
  6762. delete_macro (name)
  6763.      char *name;
  6764. {
  6765.   register int i;
  6766.   register MACRO_DEF *def;
  6767.  
  6768.   def = (MACRO_DEF *)NULL;
  6769.   for (i = 0; macro_list && (def = macro_list[i]); i++)
  6770.     if (strcmp (def->name, name) == 0)
  6771.       {
  6772.     bcopy (macro_list + i + 1, macro_list + i,
  6773.            ((macro_list_len + 1) - i) * sizeof (MACRO_DEF *));
  6774.     break;
  6775.       }
  6776.   return (def);
  6777. }
  6778.  
  6779. /* Execute the macro passed in DEF, a pointer to a MACRO_DEF. */
  6780. void
  6781. execute_macro (def)
  6782.      MACRO_DEF *def;
  6783. {
  6784.  
  6785.   if (def != (MACRO_DEF *)NULL)
  6786.     {
  6787.       char *line, *string;
  6788.  
  6789.       get_until ("\n", &line);
  6790.  
  6791.       if (curchar () == '\n')    /* as opposed to the end of the file... */
  6792.     {
  6793.       line_number++;
  6794.       input_text_offset++;
  6795.     }
  6796.       string = (char *)xmalloc (1 + strlen (def->definition) + strlen (line));
  6797.       strcpy (string, def->definition);
  6798.       strcat (string, line);
  6799.       free (line);
  6800.  
  6801.       execute_string ("%s\n", string);
  6802.       free (string);
  6803.     }
  6804. }
  6805.  
  6806. int
  6807. cm_macro ()
  6808. {
  6809.   register int i;
  6810.   char *line, *name, *expansion;
  6811.  
  6812.   get_rest_of_line (&line);
  6813.   canon_white (line);
  6814.  
  6815.   for (i = 0; line[i] && !whitespace (line[i]); i++);
  6816.   name = (char *)xmalloc (i);
  6817.   strncpy (name, line, i);
  6818.   name[i] = '\0';
  6819.  
  6820.   while (whitespace (line[i]))
  6821.     i++;
  6822.  
  6823.   add_macro (name, line + i, input_filename, line_number);
  6824.   free (line);
  6825.   free (name);
  6826. }
  6827.  
  6828. int
  6829. cm_unmacro ()
  6830. {
  6831.   register int i;
  6832.   char *line, *name;
  6833.   MACRO_DEF *def;
  6834.  
  6835.   get_rest_of_line (&line);
  6836.   canon_white (line);
  6837.  
  6838.   for (i = 0; line[i] && !whitespace (line[i]); i++);
  6839.   name = (char *)xmalloc (i);
  6840.   strncpy (name, line, i);
  6841.   name[i] = '\0';
  6842.  
  6843.   def = delete_macro (name);
  6844.  
  6845.   if (def)
  6846.     {
  6847.       free (def->filename);
  6848.       free (def->name);
  6849.       free (def->definition);
  6850.       free (def);
  6851.     }
  6852.  
  6853.   free (line);
  6854.   free (name);
  6855. }
  6856. #endif /* HAVE_MACROS */
  6857.